1 /**
2  * \file
3  * Copyright 2002-2003 Ximian Inc
4  * Copyright 2003-2011 Novell Inc
5  * Copyright 2011 Xamarin Inc
6  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8 #ifndef __MONO_AOT_RUNTIME_H__
9 #define __MONO_AOT_RUNTIME_H__
10 
11 #include "mini.h"
12 
13 /* Version number of the AOT file format */
14 #define MONO_AOT_FILE_VERSION 143
15 
16 #define MONO_AOT_TRAMP_PAGE_SIZE 16384
17 
18 /* Constants used to encode different types of methods in AOT */
19 enum {
20 	MONO_AOT_METHODREF_MIN = 240,
21 	/* Image index bigger than METHODREF_MIN */
22 	MONO_AOT_METHODREF_LARGE_IMAGE_INDEX = 249,
23 	/* Runtime provided methods on arrays */
24 	MONO_AOT_METHODREF_ARRAY = 250,
25 	MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE = 251,
26 	/* Wrappers */
27 	MONO_AOT_METHODREF_WRAPPER = 252,
28 	/* Methods on generic instances */
29 	MONO_AOT_METHODREF_GINST = 253,
30 	/* Methods resolve using a METHODSPEC token */
31 	MONO_AOT_METHODREF_METHODSPEC = 254,
32 };
33 
34 /* Constants used to encode different types of types in AOT */
35 enum {
36 	/* typedef index */
37 	MONO_AOT_TYPEREF_TYPEDEF_INDEX = 1,
38 	/* typedef index + image index */
39 	MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE = 2,
40 	/* typespec token */
41 	MONO_AOT_TYPEREF_TYPESPEC_TOKEN = 3,
42 	/* generic inst */
43 	MONO_AOT_TYPEREF_GINST = 4,
44 	/* type/method variable */
45 	MONO_AOT_TYPEREF_VAR = 5,
46 	/* array */
47 	MONO_AOT_TYPEREF_ARRAY = 6,
48 	/* blob index of the type encoding */
49 	MONO_AOT_TYPEREF_BLOB_INDEX = 7,
50 	/* ptr */
51 	MONO_AOT_TYPEREF_PTR = 8
52 };
53 
54 /* Trampolines which we have a lot of */
55 typedef enum {
56 	MONO_AOT_TRAMP_SPECIFIC = 0,
57 	MONO_AOT_TRAMP_STATIC_RGCTX = 1,
58 	MONO_AOT_TRAMP_IMT = 2,
59 	MONO_AOT_TRAMP_GSHAREDVT_ARG = 3,
60 	MONO_AOT_TRAMP_NUM = 4
61 } MonoAotTrampoline;
62 
63 typedef enum {
64 	MONO_AOT_FILE_FLAG_WITH_LLVM = 1,
65 	MONO_AOT_FILE_FLAG_FULL_AOT = 2,
66 	MONO_AOT_FILE_FLAG_DEBUG = 4,
67 	MONO_AOT_FILE_FLAG_LLVM_THUMB = 8,
68 	MONO_AOT_FILE_FLAG_LLVM_ONLY = 16,
69 	MONO_AOT_FILE_FLAG_SAFEPOINTS = 32,
70 	MONO_AOT_FILE_FLAG_SEPARATE_DATA = 64,
71 	MONO_AOT_FILE_FLAG_EAGER_LOAD = 128,
72 } MonoAotFileFlags;
73 
74 typedef enum {
75 	MONO_AOT_TABLE_BLOB,
76 	MONO_AOT_TABLE_IMAGE_TABLE,
77 	MONO_AOT_TABLE_CLASS_NAME,
78 	MONO_AOT_TABLE_METHOD_INFO_OFFSETS,
79 	MONO_AOT_TABLE_EX_INFO_OFFSETS,
80 	MONO_AOT_TABLE_CLASS_INFO_OFFSETS,
81 	MONO_AOT_TABLE_GOT_INFO_OFFSETS,
82 	MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS,
83 	MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS,
84 	MONO_AOT_TABLE_EXTRA_METHOD_TABLE,
85 	MONO_AOT_TABLE_WEAK_FIELD_INDEXES,
86 	MONO_AOT_TABLE_NUM
87 } MonoAotFileTable;
88 
89 /* This structure is stored in the AOT file */
90 typedef struct MonoAotFileInfo
91 {
92 	/* The version number of the AOT file format, should match MONO_AOT_FILE_VERSION */
93 	guint32 version;
94 	/* For alignment */
95 	guint32 dummy;
96 
97 	/* All the pointers should be at the start to avoid alignment problems */
98 	/* Symbols */
99 #define MONO_AOT_FILE_INFO_FIRST_SYMBOL jit_got
100 	/* Global Offset Table for JITted code */
101 	gpointer jit_got;
102 	/* Global Offset Table for LLVM code */
103 	gpointer llvm_got;
104 	/* Mono EH Frame created by llc when using LLVM */
105 	gpointer mono_eh_frame;
106 	/* Points to the get_method () function in the LLVM image or NULL */
107 	gpointer llvm_get_method;
108 	/* Points to the get_unbox_tramp () function in the LLVM image or NULL */
109 	gpointer llvm_get_unbox_tramp;
110 	gpointer jit_code_start;
111 	gpointer jit_code_end;
112 	gpointer method_addresses;
113 
114 	/*
115 	 * Data tables.
116 	 * One pointer for each entry in MonoAotFileTable.
117 	 */
118 	/* Data blob */
119 	gpointer blob;
120 	gpointer class_name_table;
121 	gpointer class_info_offsets;
122 	gpointer method_info_offsets;
123 	gpointer ex_info_offsets;
124 	gpointer extra_method_info_offsets;
125 	gpointer extra_method_table;
126 	gpointer got_info_offsets;
127 	gpointer llvm_got_info_offsets;
128 	gpointer image_table;
129 	/* Points to an array of weak field indexes */
130 	gpointer weak_field_indexes;
131 
132 	gpointer mem_end;
133 	/* The GUID of the assembly which the AOT image was generated from */
134 	gpointer assembly_guid;
135 	/*
136 	 * The runtime version string for AOT images generated using 'bind-to-runtime-version',
137 	 * NULL otherwise.
138 	 */
139 	gpointer runtime_version;
140 	/* Blocks of various kinds of trampolines */
141 	gpointer specific_trampolines;
142 	gpointer static_rgctx_trampolines;
143 	gpointer imt_trampolines;
144 	gpointer gsharedvt_arg_trampolines;
145 	/* In static mode, points to a table of global symbols for trampolines etc */
146 	gpointer globals;
147 	/* Points to a string containing the assembly name*/
148 	gpointer assembly_name;
149 	/* Start of Mono's Program Linkage Table */
150 	gpointer plt;
151 	/* End of Mono's Program Linkage Table */
152 	gpointer plt_end;
153 	gpointer unwind_info;
154 	/* Points to a table mapping methods to their unbox trampolines */
155 	gpointer unbox_trampolines;
156 	/* Points to the end of the previous table */
157 	gpointer unbox_trampolines_end;
158 	/* Points to a table of unbox trampoline addresses/offsets */
159 	gpointer unbox_trampoline_addresses;
160 #define MONO_AOT_FILE_INFO_LAST_SYMBOL unbox_trampoline_addresses
161 
162 	/* Scalars */
163 	/* The index of the first GOT slot used by the PLT */
164 	guint32 plt_got_offset_base;
165 	/* Number of entries in the GOT */
166 	guint32 got_size;
167 	/* Number of entries in the PLT */
168 	guint32 plt_size;
169 	/* Number of methods */
170 	guint32 nmethods;
171 	/* A union of MonoAotFileFlags */
172 	guint32 flags;
173 	/* Optimization flags used to compile the module */
174 	guint32 opts;
175 	/* SIMD flags used to compile the module */
176 	guint32 simd_opts;
177 	/* Index of the blob entry holding the GC used by this module */
178 	gint32 gc_name_index;
179 	guint32 num_rgctx_fetch_trampolines;
180 	/* These are used for sanity checking object layout problems when cross-compiling */
181 	guint32 double_align, long_align, generic_tramp_num;
182 	/* The page size used by trampoline pages */
183 	guint32 tramp_page_size;
184 	/*
185 	 * The number of GOT entries which need to be preinitialized when the
186 	 * module is loaded.
187 	 */
188 	guint32 nshared_got_entries;
189 	/* The size of the data file, if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
190 	guint32 datafile_size;
191 
192 	/* Arrays */
193 	/* Offsets for tables inside the data file if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
194 	// FIXME: Sync with AOT
195 	guint32 table_offsets [MONO_AOT_TABLE_NUM];
196 	/* Number of trampolines */
197 	guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
198 	/* The indexes of the first GOT slots used by the trampolines */
199 	guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
200 	/* The size of one trampoline */
201 	guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
202 	/* The offset where the trampolines begin on a trampoline page */
203 	guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
204 	/* GUID of aot compilation */
205 	guint8 aotid[16];
206 } MonoAotFileInfo;
207 
208 /* Number of symbols in the MonoAotFileInfo structure */
209 #define MONO_AOT_FILE_INFO_NUM_SYMBOLS (((G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_LAST_SYMBOL) - G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_FIRST_SYMBOL)) / sizeof (gpointer)) + 1)
210 
211 void      mono_aot_init                     (void);
212 void      mono_aot_cleanup                  (void);
213 gpointer  mono_aot_get_method_checked       (MonoDomain *domain,
214 											 MonoMethod *method, MonoError *error);
215 gpointer  mono_aot_get_method_from_token    (MonoDomain *domain, MonoImage *image, guint32 token, MonoError *error);
216 gboolean  mono_aot_is_got_entry             (guint8 *code, guint8 *addr);
217 guint8*   mono_aot_get_plt_entry            (guint8 *code);
218 guint32   mono_aot_get_plt_info_offset      (mgreg_t *regs, guint8 *code);
219 gboolean  mono_aot_get_cached_class_info    (MonoClass *klass, MonoCachedClassInfo *res);
220 gboolean  mono_aot_get_class_from_name      (MonoImage *image, const char *name_space, const char *name, MonoClass **klass);
221 MonoJitInfo* mono_aot_find_jit_info         (MonoDomain *domain, MonoImage *image, gpointer addr);
222 gpointer mono_aot_plt_resolve               (gpointer aot_module, guint32 plt_info_offset, guint8 *code, MonoError *error);
223 void     mono_aot_patch_plt_entry           (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr);
224 gpointer mono_aot_get_method_from_vt_slot   (MonoDomain *domain, MonoVTable *vtable, int slot, MonoError *error);
225 gpointer mono_aot_create_specific_trampoline   (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len);
226 gpointer mono_aot_get_trampoline            (const char *name);
227 gpointer mono_aot_get_trampoline_full       (const char *name, MonoTrampInfo **out_tinfo);
228 gpointer mono_aot_get_unbox_trampoline      (MonoMethod *method);
229 gpointer mono_aot_get_lazy_fetch_trampoline (guint32 slot);
230 gpointer mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr);
231 gpointer mono_aot_get_imt_trampoline        (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp);
232 gpointer mono_aot_get_gsharedvt_arg_trampoline(gpointer arg, gpointer addr);
233 guint8*  mono_aot_get_unwind_info           (MonoJitInfo *ji, guint32 *unwind_info_len);
234 guint32  mono_aot_method_hash               (MonoMethod *method);
235 gboolean mono_aot_can_dedup                 (MonoMethod *method);
236 MonoMethod* mono_aot_get_array_helper_from_wrapper (MonoMethod *method);
237 void     mono_aot_set_make_unreadable       (gboolean unreadable);
238 gboolean mono_aot_is_pagefault              (void *ptr);
239 void     mono_aot_handle_pagefault          (void *ptr);
240 void     mono_aot_register_jit_icall        (const char *name, gpointer addr);
241 guint32  mono_aot_find_method_index         (MonoMethod *method);
242 void     mono_aot_init_llvm_method          (gpointer aot_module, guint32 method_index);
243 void     mono_aot_init_gshared_method_this  (gpointer aot_module, guint32 method_index, MonoObject *this_ins);
244 void     mono_aot_init_gshared_method_mrgctx  (gpointer aot_module, guint32 method_index, MonoMethodRuntimeGenericContext *rgctx);
245 void     mono_aot_init_gshared_method_vtable  (gpointer aot_module, guint32 method_index, MonoVTable *vtable);
246 GHashTable *mono_aot_get_weak_field_indexes (MonoImage *image);
247 
248 /* This is an exported function */
249 MONO_API void     mono_aot_register_module           (gpointer *aot_info);
250 
251 /* These are used to load the AOT data for aot images compiled with MONO_AOT_FILE_FLAG_SEPARATE_DATA */
252 /*
253  * Return the AOT data for ASSEMBLY. SIZE is the size of the data. OUT_HANDLE should be set to a handle which is later
254  * passed to the free function.
255  */
256 typedef unsigned char* (*MonoLoadAotDataFunc)          (MonoAssembly *assembly, int size, gpointer user_data, void **out_handle);
257 /* Not yet used */
258 typedef void  (*MonoFreeAotDataFunc)          (MonoAssembly *assembly, int size, gpointer user_data, void *handle);
259 MONO_API void mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func, MonoFreeAotDataFunc free_func, gpointer user_data);
260 
261 #endif /* __MONO_AOT_RUNTIME_H__ */
262