2008-11-04 Zoltan Varga <vargaz@gmail.com>
[mono-project.git] / mono / mini / aot-runtime.c
blob4a737c069901e02c306f3e019b07b713637e0643
1 /*
2 * aot.c: mono Ahead of Time compiler
4 * Author:
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
9 */
11 #include "config.h"
12 #include <sys/types.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <fcntl.h>
17 #include <string.h>
18 #ifndef PLATFORM_WIN32
19 #include <sys/mman.h>
20 #else
21 #include <winsock2.h>
22 #include <windows.h>
23 #endif
25 #ifdef HAVE_EXECINFO_H
26 #include <execinfo.h>
27 #endif
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <limits.h> /* for PAGESIZE */
32 #ifndef PAGESIZE
33 #define PAGESIZE 4096
34 #endif
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
38 #endif
40 #include <mono/metadata/tabledefs.h>
41 #include <mono/metadata/class.h>
42 #include <mono/metadata/object.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/appdomain.h>
45 #include <mono/metadata/debug-helpers.h>
46 #include <mono/metadata/assembly.h>
47 #include <mono/metadata/metadata-internals.h>
48 #include <mono/metadata/marshal.h>
49 #include <mono/metadata/gc-internal.h>
50 #include <mono/metadata/monitor.h>
51 #include <mono/utils/mono-logger.h>
52 #include "mono/utils/mono-compiler.h"
54 #include "mini.h"
55 #include "version.h"
57 #ifndef DISABLE_AOT
59 #ifdef PLATFORM_WIN32
60 #define SHARED_EXT ".dll"
61 #elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
62 #define SHARED_EXT ".dylib"
63 #else
64 #define SHARED_EXT ".so"
65 #endif
67 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
68 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
70 typedef struct MonoAotModule {
71 char *aot_name;
72 /* Optimization flags used to compile the module */
73 guint32 opts;
74 /* Pointer to the Global Offset Table */
75 gpointer *got;
76 guint32 got_size;
77 GHashTable *name_cache;
78 GHashTable *extra_methods;
79 /* Maps methods to their code */
80 GHashTable *method_to_code;
81 MonoAssemblyName *image_names;
82 char **image_guids;
83 MonoAssembly *assembly;
84 MonoImage **image_table;
85 guint32 image_table_len;
86 gboolean out_of_date;
87 gboolean plt_inited;
88 guint8 *mem_begin;
89 guint8 *mem_end;
90 guint8 *code;
91 guint8 *code_end;
92 guint8 *plt;
93 guint8 *plt_end;
94 guint8 *plt_info;
95 guint8 *plt_jump_table;
96 guint32 plt_jump_table_size;
97 guint32 *code_offsets;
98 guint8 *method_info;
99 guint32 *method_info_offsets;
100 guint8 *got_info;
101 guint32 *got_info_offsets;
102 guint8 *ex_info;
103 guint32 *ex_info_offsets;
104 guint32 *method_order;
105 guint32 *method_order_end;
106 guint8 *class_info;
107 guint32 *class_info_offsets;
108 guint32 *methods_loaded;
109 guint16 *class_name_table;
110 guint32 *extra_method_table;
111 guint32 *extra_method_info_offsets;
112 guint8 *extra_method_info;
113 guint8 *trampolines;
114 guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
115 gpointer *globals;
116 MonoDl *sofile;
117 } MonoAotModule;
119 static GHashTable *aot_modules;
120 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
121 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
122 static CRITICAL_SECTION aot_mutex;
125 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
126 * AOT modules registered by mono_aot_register_module ().
128 static GHashTable *static_aot_modules;
131 * Disabling this will make a copy of the loaded code and use the copy instead
132 * of the original. This will place the caller and the callee close to each
133 * other in memory, possibly improving cache behavior. Since the original
134 * code is in copy-on-write memory, this will not increase the memory usage
135 * of the runtime.
137 static gboolean use_loaded_code = TRUE;
140 * Whenever to AOT compile loaded assemblies on demand and store them in
141 * a cache under $HOME/.mono/aot-cache.
143 static gboolean use_aot_cache = FALSE;
146 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
147 * use_aot_cache is TRUE.
149 static gboolean spawn_compiler = TRUE;
151 /* For debugging */
152 static gint32 mono_last_aot_method = -1;
154 static gboolean make_unreadable = FALSE;
155 static guint32 n_pagefaults = 0;
156 static guint32 name_table_accesses = 0;
158 /* Used to speed-up find_aot_module () */
159 static gsize aot_code_low_addr = (gssize)-1;
160 static gsize aot_code_high_addr = 0;
162 /* Used to communicate with mono_aot_register_globals () */
163 static guint32 globals_tls_id = -1;
165 static void
166 init_plt (MonoAotModule *info);
168 static MonoJumpInfo*
169 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
170 guint32 got_index, guint32 **got_slots,
171 guint8 *buf, guint8 **endbuf);
173 static inline gboolean
174 is_got_patch (MonoJumpInfoType patch_type)
176 return TRUE;
179 /*****************************************************/
180 /* AOT RUNTIME */
181 /*****************************************************/
183 static MonoImage *
184 load_image (MonoAotModule *module, int index)
186 MonoAssembly *assembly;
187 MonoImageOpenStatus status;
189 g_assert (index < module->image_table_len);
191 if (module->image_table [index])
192 return module->image_table [index];
193 if (module->out_of_date)
194 return NULL;
196 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
197 if (!assembly) {
198 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
199 module->out_of_date = TRUE;
200 return NULL;
203 if (strcmp (assembly->image->guid, module->image_guids [index])) {
204 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name);
205 module->out_of_date = TRUE;
206 return NULL;
209 module->image_table [index] = assembly->image;
210 return assembly->image;
214 static inline gint32
215 decode_value (guint8 *ptr, guint8 **rptr)
217 guint8 b = *ptr;
218 gint32 len;
220 if ((b & 0x80) == 0){
221 len = b;
222 ++ptr;
223 } else if ((b & 0x40) == 0){
224 len = ((b & 0x3f) << 8 | ptr [1]);
225 ptr += 2;
226 } else if (b != 0xff) {
227 len = ((b & 0x1f) << 24) |
228 (ptr [1] << 16) |
229 (ptr [2] << 8) |
230 ptr [3];
231 ptr += 4;
233 else {
234 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
235 ptr += 5;
237 if (rptr)
238 *rptr = ptr;
240 //printf ("DECODE: %d.\n", len);
241 return len;
244 static MonoMethod*
245 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
247 static MonoClass*
248 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
250 static MonoGenericInst*
251 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
253 int type_argc, i;
254 MonoType **type_argv;
255 MonoGenericInst *inst;
256 guint8 *p = buf;
258 type_argc = decode_value (p, &p);
259 type_argv = g_new0 (MonoType*, type_argc);
261 for (i = 0; i < type_argc; ++i) {
262 MonoClass *pclass = decode_klass_ref (module, p, &p);
263 if (!pclass) {
264 g_free (type_argv);
265 return NULL;
267 type_argv [i] = &pclass->byval_arg;
270 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
271 g_free (type_argv);
273 *endbuf = p;
275 return inst;
278 static gboolean
279 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
281 gboolean has_class_inst, has_method_inst;
282 guint8 *p = buf;
284 has_class_inst = decode_value (p, &p);
285 if (has_class_inst) {
286 ctx->class_inst = decode_generic_inst (module, p, &p);
287 if (!ctx->class_inst)
288 return FALSE;
290 has_method_inst = decode_value (p, &p);
291 if (has_method_inst) {
292 ctx->method_inst = decode_generic_inst (module, p, &p);
293 if (!ctx->method_inst)
294 return FALSE;
297 *endbuf = p;
298 return TRUE;
301 static MonoClass*
302 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
304 MonoImage *image;
305 MonoClass *klass, *eklass;
306 guint32 token, rank;
307 guint8 *p = buf;
309 token = decode_value (p, &p);
310 if (token == 0) {
311 *endbuf = p;
312 return NULL;
314 if (mono_metadata_token_table (token) == 0) {
315 image = load_image (module, decode_value (p, &p));
316 if (!image)
317 return NULL;
318 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
319 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
320 if (token == MONO_TOKEN_TYPE_SPEC) {
321 MonoTypeEnum type = decode_value (p, &p);
323 if (type == MONO_TYPE_GENERICINST) {
324 MonoClass *gclass;
325 MonoGenericContext ctx;
326 MonoType *type;
328 gclass = decode_klass_ref (module, p, &p);
329 g_assert (gclass->generic_container);
331 memset (&ctx, 0, sizeof (ctx));
332 ctx.class_inst = decode_generic_inst (module, p, &p);
333 if (!ctx.class_inst)
334 return NULL;
335 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
336 klass = mono_class_from_mono_type (type);
337 mono_metadata_free_type (type);
338 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
339 MonoType *t;
340 gboolean is_method;
341 MonoGenericContainer *container;
343 // FIXME: Maybe use types directly to avoid
344 // the overhead of creating MonoClass-es
346 // FIXME: Memory management
347 t = g_new0 (MonoType, 1);
348 t->type = type;
349 t->data.generic_param = g_new0 (MonoGenericParam, 1);
350 t->data.generic_param->num = decode_value (p, &p);
351 t->data.generic_param->name = "T";
353 is_method = decode_value (p, &p);
354 if (is_method) {
355 MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
357 if (!method_def) {
358 g_free (t->data.generic_param);
359 g_free (t);
360 return NULL;
363 container = mono_method_get_generic_container (method_def);
364 } else {
365 MonoClass *class_def = decode_klass_ref (module, p, &p);
367 if (!class_def) {
368 g_free (t->data.generic_param);
369 g_free (t);
370 return NULL;
373 container = class_def->generic_container;
376 g_assert (container);
377 t->data.generic_param->owner = container;
379 klass = mono_class_from_mono_type (t);
380 } else {
381 g_assert_not_reached ();
383 } else {
384 image = load_image (module, decode_value (p, &p));
385 if (!image)
386 return NULL;
387 klass = mono_class_get (image, token);
389 } else if (token == MONO_TOKEN_TYPE_DEF) {
390 /* Array */
391 image = load_image (module, decode_value (p, &p));
392 if (!image)
393 return NULL;
394 rank = decode_value (p, &p);
395 eklass = decode_klass_ref (module, p, &p);
396 klass = mono_array_class_get (eklass, rank);
397 } else {
398 g_assert_not_reached ();
400 g_assert (klass);
401 mono_class_init (klass);
403 *endbuf = p;
404 return klass;
407 static MonoClassField*
408 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
410 MonoClass *klass = decode_klass_ref (module, buf, &buf);
411 guint32 token;
412 guint8 *p = buf;
414 if (!klass)
415 return NULL;
417 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
419 *endbuf = p;
421 return mono_class_get_field (klass, token);
425 * decode_method_ref:
427 * Decode a method reference, and return its image and token. This avoids loading
428 * metadata for the method if the caller does not need it. If the method has no token,
429 * then it is loaded from metadata and METHOD is set to the method instance.
431 static MonoImage*
432 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
434 guint32 image_index, value;
435 MonoImage *image;
436 guint8 *p = buf;
438 if (method)
439 *method = NULL;
440 if (no_aot_trampoline)
441 *no_aot_trampoline = FALSE;
443 value = decode_value (p, &p);
444 image_index = value >> 24;
446 if (image_index == 252) {
447 if (no_aot_trampoline)
448 *no_aot_trampoline = TRUE;
449 value = decode_value (p, &p);
450 image_index = value >> 24;
453 if (image_index == 253) {
454 /* Wrapper */
455 guint32 wrapper_type;
457 wrapper_type = decode_value (p, &p);
459 /* Doesn't matter */
460 image = mono_defaults.corlib;
462 switch (wrapper_type) {
463 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
464 MonoMethod *m = decode_method_ref_2 (module, p, &p);
466 if (!m)
467 return NULL;
468 mono_class_init (m->klass);
469 *method = mono_marshal_get_remoting_invoke_with_check (m);
470 break;
472 case MONO_WRAPPER_PROXY_ISINST: {
473 MonoClass *klass = decode_klass_ref (module, p, &p);
474 if (!klass)
475 return NULL;
476 *method = mono_marshal_get_proxy_cancast (klass);
477 break;
479 case MONO_WRAPPER_LDFLD:
480 case MONO_WRAPPER_LDFLDA:
481 case MONO_WRAPPER_STFLD:
482 case MONO_WRAPPER_ISINST: {
483 MonoClass *klass = decode_klass_ref (module, p, &p);
484 if (!klass)
485 return NULL;
486 if (wrapper_type == MONO_WRAPPER_LDFLD)
487 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
488 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
489 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
490 else if (wrapper_type == MONO_WRAPPER_STFLD)
491 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
492 else if (wrapper_type == MONO_WRAPPER_ISINST)
493 *method = mono_marshal_get_isinst (klass);
494 else
495 g_assert_not_reached ();
496 break;
498 case MONO_WRAPPER_LDFLD_REMOTE:
499 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
500 break;
501 case MONO_WRAPPER_STFLD_REMOTE:
502 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
503 break;
504 case MONO_WRAPPER_ALLOC: {
505 int atype = decode_value (p, &p);
507 *method = mono_gc_get_managed_allocator_by_type (atype);
508 break;
510 case MONO_WRAPPER_STELEMREF:
511 *method = mono_marshal_get_stelemref ();
512 break;
513 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
514 MonoMethod *m = decode_method_ref_2 (module, p, &p);
516 if (!m)
517 return NULL;
518 *method = mono_marshal_get_static_rgctx_invoke (m);
519 break;
521 case MONO_WRAPPER_MONITOR_FAST_ENTER:
522 case MONO_WRAPPER_MONITOR_FAST_EXIT: {
523 MonoMethodDesc *desc;
524 MonoMethod *orig_method;
526 if (wrapper_type == MONO_WRAPPER_MONITOR_FAST_ENTER)
527 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
528 else
529 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
530 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
531 g_assert (orig_method);
532 mono_method_desc_free (desc);
533 *method = mono_monitor_get_fast_path (orig_method);
534 break;
536 default:
537 g_assert_not_reached ();
539 } else if (image_index == 255) {
540 /* Methodspec */
541 image_index = decode_value (p, &p);
542 *token = decode_value (p, &p);
544 image = load_image (module, image_index);
545 if (!image)
546 return NULL;
547 } else if (image_index == 254) {
548 /* Method on generic instance */
549 MonoClass *klass;
550 MonoGenericContext ctx;
553 * These methods do not have a token which resolves them, so we
554 * resolve them immediately.
556 klass = decode_klass_ref (module, p, &p);
557 if (!klass)
558 return NULL;
560 image_index = decode_value (p, &p);
561 *token = decode_value (p, &p);
563 image = load_image (module, image_index);
564 if (!image)
565 return NULL;
567 *method = mono_get_method_full (image, *token, NULL, NULL);
568 if (!(*method))
569 return NULL;
571 memset (&ctx, 0, sizeof (ctx));
573 if (FALSE && klass->generic_class) {
574 ctx.class_inst = klass->generic_class->context.class_inst;
575 ctx.method_inst = NULL;
577 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
580 memset (&ctx, 0, sizeof (ctx));
582 if (!decode_generic_context (module, &ctx, p, &p))
583 return NULL;
585 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
586 } else {
587 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
589 image = load_image (module, image_index);
590 if (!image)
591 return NULL;
594 *endbuf = p;
596 return image;
600 * decode_method_ref_2:
602 * Similar to decode_method_ref, but resolve and return the method itself.
604 static MonoMethod*
605 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
607 MonoMethod *method;
608 guint32 token;
609 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
611 if (method)
612 return method;
613 if (!image)
614 return NULL;
615 return mono_get_method (image, token, NULL);
618 G_GNUC_UNUSED
619 static void
620 make_writable (guint8* addr, guint32 len)
622 #ifndef PLATFORM_WIN32
623 guint8 *page_start;
624 int pages, err;
626 if (mono_aot_only)
627 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
629 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
630 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
631 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
632 g_assert (err == 0);
633 #else
635 DWORD oldp;
636 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
638 #endif
641 static void
642 create_cache_structure (void)
644 const char *home;
645 char *tmp;
646 int err;
648 home = g_get_home_dir ();
649 if (!home)
650 return;
652 tmp = g_build_filename (home, ".mono", NULL);
653 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
654 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
655 #ifdef PLATFORM_WIN32
656 err = mkdir (tmp);
657 #else
658 err = mkdir (tmp, 0777);
659 #endif
660 if (err) {
661 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
662 g_free (tmp);
663 return;
666 g_free (tmp);
667 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
668 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
669 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
670 #ifdef PLATFORM_WIN32
671 err = mkdir (tmp);
672 #else
673 err = mkdir (tmp, 0777);
674 #endif
675 if (err) {
676 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
677 g_free (tmp);
678 return;
681 g_free (tmp);
685 * load_aot_module_from_cache:
687 * Experimental code to AOT compile loaded assemblies on demand.
689 * FIXME:
690 * - Add environment variable MONO_AOT_CACHE_OPTIONS
691 * - Add options for controlling the cache size
692 * - Handle full cache by deleting old assemblies lru style
693 * - Add options for excluding assemblies during development
694 * - Maybe add a threshold after an assembly is AOT compiled
695 * - invoking a new mono process is a security risk
696 * - recompile the AOT module if one of its dependencies changes
698 static MonoDl*
699 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
701 char *fname, *cmd, *tmp2, *aot_options;
702 const char *home;
703 MonoDl *module;
704 gboolean res;
705 gchar *out, *err;
706 gint exit_status;
708 *aot_name = NULL;
710 if (assembly->image->dynamic)
711 return NULL;
713 create_cache_structure ();
715 home = g_get_home_dir ();
717 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
718 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
719 *aot_name = fname;
720 g_free (tmp2);
722 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
723 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
725 if (!module) {
726 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
728 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
730 aot_options = g_strdup_printf ("outfile=%s", fname);
732 if (spawn_compiler) {
733 /* FIXME: security */
734 /* FIXME: Has to pass the assembly loading path to the child process */
735 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
737 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
739 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
740 if (res) {
741 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
742 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
743 else
744 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
745 g_free (out);
746 g_free (err);
748 #endif
749 g_free (cmd);
750 } else {
751 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
752 if (!res) {
753 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
754 } else {
755 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
759 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
761 g_free (aot_options);
764 return module;
767 static void
768 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
770 if (globals) {
771 int i = 0;
773 *value = NULL;
774 for (i = 0; globals [i]; i+= 2) {
775 if (strcmp (globals [i], name) == 0) {
776 *value = globals [i + 1];
777 break;
780 } else {
781 mono_dl_symbol (module, name, value);
785 static void
786 load_aot_module (MonoAssembly *assembly, gpointer user_data)
788 char *aot_name;
789 MonoAotModule *amodule;
790 MonoDl *sofile;
791 gboolean usable = TRUE;
792 char *saved_guid = NULL;
793 char *aot_version = NULL;
794 char *runtime_version;
795 char *opt_flags = NULL;
796 gpointer *globals;
797 gboolean full_aot = FALSE;
798 gpointer *plt_jump_table_addr = NULL;
799 guint32 *plt_jump_table_size = NULL;
800 guint32 *trampolines_info = NULL;
801 gpointer *got_addr = NULL;
802 gpointer *got = NULL;
803 guint32 *got_size_ptr = NULL;
804 int i;
806 if (mono_compile_aot)
807 return;
809 if (assembly->image->aot_module)
811 * Already loaded. This can happen because the assembly loading code might invoke
812 * the assembly load hooks multiple times for the same assembly.
814 return;
816 if (assembly->image->dynamic)
817 return;
819 mono_aot_lock ();
820 if (static_aot_modules)
821 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
822 else
823 globals = NULL;
824 mono_aot_unlock ();
826 if (globals) {
827 /* Statically linked AOT module */
828 sofile = NULL;
829 aot_name = g_strdup_printf ("%s", assembly->aname.name);
830 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
831 } else {
832 TlsSetValue (globals_tls_id, NULL);
834 if (use_aot_cache)
835 sofile = load_aot_module_from_cache (assembly, &aot_name);
836 else {
837 char *err;
838 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
840 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
842 if (!sofile) {
843 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
844 g_free (err);
849 * If the image was compiled in no-dlsym mode, it contains no global symbols,
850 * instead it contains an ELF ctor function which is called by dlopen () which
851 * in turn calls mono_aot_register_globals () to register a table which contains
852 * the name and address of the globals.
854 globals = TlsGetValue (globals_tls_id);
855 TlsSetValue (globals_tls_id, NULL);
858 if (!sofile && !globals) {
859 if (mono_aot_only) {
860 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
861 exit (1);
863 g_free (aot_name);
864 return;
867 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
868 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
869 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
870 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
872 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
873 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
874 usable = FALSE;
876 else {
877 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
878 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
879 usable = FALSE;
883 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, FULL_VERSION)))) {
884 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against runtime version %s while this runtime has version %s.\n", aot_name, runtime_version, FULL_VERSION);
885 usable = FALSE;
889 char *full_aot_str;
891 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
893 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
894 full_aot = TRUE;
897 if (mono_aot_only && !full_aot) {
898 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
899 exit (1);
901 if (!mono_aot_only && full_aot) {
902 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
903 usable = FALSE;
906 if (!usable) {
907 if (mono_aot_only) {
908 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
909 exit (1);
911 g_free (aot_name);
912 if (sofile)
913 mono_dl_close (sofile);
914 assembly->image->aot_module = NULL;
915 return;
918 find_symbol (sofile, globals, "got_addr", (gpointer *)&got_addr);
919 g_assert (got_addr);
920 got = (gpointer*)*got_addr;
921 g_assert (got);
922 find_symbol (sofile, globals, "got_size", (gpointer *)&got_size_ptr);
923 g_assert (got_size_ptr);
925 amodule = g_new0 (MonoAotModule, 1);
926 amodule->aot_name = aot_name;
927 amodule->assembly = assembly;
928 amodule->got = got;
929 amodule->got_size = *got_size_ptr;
930 amodule->got [0] = assembly->image;
931 amodule->globals = globals;
932 amodule->sofile = sofile;
933 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
935 sscanf (opt_flags, "%d", &amodule->opts);
937 /* Read image table */
939 guint32 table_len, i;
940 char *table = NULL;
942 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
943 g_assert (table);
945 table_len = *(guint32*)table;
946 table += sizeof (guint32);
947 amodule->image_table = g_new0 (MonoImage*, table_len);
948 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
949 amodule->image_guids = g_new0 (char*, table_len);
950 amodule->image_table_len = table_len;
951 for (i = 0; i < table_len; ++i) {
952 MonoAssemblyName *aname = &(amodule->image_names [i]);
954 aname->name = g_strdup (table);
955 table += strlen (table) + 1;
956 amodule->image_guids [i] = g_strdup (table);
957 table += strlen (table) + 1;
958 if (table [0] != 0)
959 aname->culture = g_strdup (table);
960 table += strlen (table) + 1;
961 memcpy (aname->public_key_token, table, strlen (table) + 1);
962 table += strlen (table) + 1;
964 table = ALIGN_PTR_TO (table, 8);
965 aname->flags = *(guint32*)table;
966 table += 4;
967 aname->major = *(guint32*)table;
968 table += 4;
969 aname->minor = *(guint32*)table;
970 table += 4;
971 aname->build = *(guint32*)table;
972 table += 4;
973 aname->revision = *(guint32*)table;
974 table += 4;
978 /* Read method and method_info tables */
979 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
980 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
981 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
982 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
983 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
984 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
985 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
986 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
987 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
988 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
989 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
990 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
991 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
992 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
993 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
994 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
995 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
996 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
997 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
999 amodule->mem_begin = amodule->code;
1001 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1002 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1003 find_symbol (sofile, globals, "plt_info", (gpointer*)&amodule->plt_info);
1005 find_symbol (sofile, globals, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
1006 g_assert (plt_jump_table_addr);
1007 amodule->plt_jump_table = (guint8*)*plt_jump_table_addr;
1008 g_assert (amodule->plt_jump_table);
1010 find_symbol (sofile, globals, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
1011 g_assert (plt_jump_table_size);
1012 amodule->plt_jump_table_size = *plt_jump_table_size;
1014 find_symbol (sofile, globals, "trampolines_info", (gpointer *)&trampolines_info);
1015 if (trampolines_info) {
1016 amodule->num_trampolines = trampolines_info [0];
1017 amodule->first_trampoline_got_offset = trampolines_info [1];
1020 if (make_unreadable) {
1021 #ifndef PLATFORM_WIN32
1022 guint8 *addr;
1023 guint8 *page_start;
1024 int pages, err, len;
1026 addr = amodule->mem_begin;
1027 len = amodule->mem_end - amodule->mem_begin;
1029 /* Round down in both directions to avoid modifying data which is not ours */
1030 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1031 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1032 err = mprotect (page_start, pages * PAGESIZE, 0);
1033 g_assert (err == 0);
1034 #endif
1037 mono_aot_lock ();
1039 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1040 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1042 g_hash_table_insert (aot_modules, assembly, amodule);
1043 mono_aot_unlock ();
1045 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1047 assembly->image->aot_module = amodule;
1050 * Since we store methoddef and classdef tokens when referring to methods/classes in
1051 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1052 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1053 * non-lazily, since we can't handle out-of-date errors later.
1055 for (i = 0; i < amodule->image_table_len; ++i)
1056 load_image (amodule, i);
1058 if (amodule->out_of_date) {
1059 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
1060 if (mono_aot_only) {
1061 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode because a dependency cannot be found or it is out of date.\n", aot_name);
1062 exit (1);
1065 else
1066 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1070 * mono_aot_register_globals:
1072 * This is called by the ctor function in AOT images compiled with the
1073 * 'no-dlsym' option.
1075 void
1076 mono_aot_register_globals (gpointer *globals)
1078 TlsSetValue (globals_tls_id, globals);
1082 * mono_aot_register_module:
1084 * This should be called by embedding code to register AOT modules statically linked
1085 * into the executable. AOT_INFO should be the value of the
1086 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1088 void
1089 mono_aot_register_module (gpointer *aot_info)
1091 gpointer *globals;
1092 char *aname;
1094 globals = aot_info;
1095 g_assert (globals);
1097 /* Determine the assembly name */
1098 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1099 g_assert (aname);
1101 /* This could be called before startup */
1102 if (aot_modules)
1103 mono_aot_lock ();
1105 if (!static_aot_modules)
1106 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1108 g_hash_table_insert (static_aot_modules, aname, globals);
1110 if (aot_modules)
1111 mono_aot_unlock ();
1114 void
1115 mono_aot_init (void)
1117 InitializeCriticalSection (&aot_mutex);
1118 aot_modules = g_hash_table_new (NULL, NULL);
1119 globals_tls_id = TlsAlloc ();
1121 mono_install_assembly_load_hook (load_aot_module, NULL);
1123 if (getenv ("MONO_LASTAOT"))
1124 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1125 if (getenv ("MONO_AOT_CACHE"))
1126 use_aot_cache = TRUE;
1129 static gboolean
1130 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1132 guint32 flags;
1134 info->vtable_size = decode_value (buf, &buf);
1135 if (info->vtable_size == -1)
1136 /* Generic type */
1137 return FALSE;
1138 flags = decode_value (buf, &buf);
1139 info->ghcimpl = (flags >> 0) & 0x1;
1140 info->has_finalize = (flags >> 1) & 0x1;
1141 info->has_cctor = (flags >> 2) & 0x1;
1142 info->has_nested_classes = (flags >> 3) & 0x1;
1143 info->blittable = (flags >> 4) & 0x1;
1144 info->has_references = (flags >> 5) & 0x1;
1145 info->has_static_refs = (flags >> 6) & 0x1;
1146 info->no_special_static_fields = (flags >> 7) & 0x1;
1148 if (info->has_cctor) {
1149 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1150 if (!cctor_image)
1151 return FALSE;
1153 if (info->has_finalize) {
1154 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1155 if (!info->finalize_image)
1156 return FALSE;
1159 info->instance_size = decode_value (buf, &buf);
1160 info->class_size = decode_value (buf, &buf);
1161 info->packing_size = decode_value (buf, &buf);
1162 info->min_align = decode_value (buf, &buf);
1164 *endbuf = buf;
1166 return TRUE;
1169 gpointer
1170 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1172 int i;
1173 MonoClass *klass = vtable->klass;
1174 MonoAotModule *aot_module = klass->image->aot_module;
1175 guint8 *info, *p;
1176 MonoCachedClassInfo class_info;
1177 gboolean err;
1178 guint32 token;
1179 MonoImage *image;
1180 gboolean no_aot_trampoline;
1182 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1183 return NULL;
1185 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1186 p = info;
1188 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1189 if (!err)
1190 return NULL;
1192 for (i = 0; i < slot; ++i)
1193 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1195 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1196 if (!image)
1197 return NULL;
1198 if (no_aot_trampoline)
1199 return NULL;
1201 if (mono_metadata_token_index (token) == 0)
1202 return NULL;
1204 return mono_aot_get_method_from_token (domain, image, token);
1207 gboolean
1208 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1210 MonoAotModule *aot_module = klass->image->aot_module;
1211 guint8 *p;
1212 gboolean err;
1214 if (klass->rank || !aot_module)
1215 return FALSE;
1217 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1219 err = decode_cached_class_info (aot_module, res, p, &p);
1220 if (!err)
1221 return FALSE;
1223 return TRUE;
1227 * mono_aot_get_class_from_name:
1229 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1230 * using a cache stored in the AOT file.
1231 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1233 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1234 * found.
1236 gboolean
1237 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1239 MonoAotModule *aot_module = image->aot_module;
1240 guint16 *table, *entry;
1241 guint16 table_size;
1242 guint32 hash;
1243 char full_name_buf [1024];
1244 char *full_name;
1245 const char *name2, *name_space2;
1246 MonoTableInfo *t;
1247 guint32 cols [MONO_TYPEDEF_SIZE];
1248 GHashTable *nspace_table;
1250 if (!aot_module || !aot_module->class_name_table)
1251 return FALSE;
1253 mono_aot_lock ();
1255 *klass = NULL;
1257 /* First look in the cache */
1258 if (!aot_module->name_cache)
1259 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1260 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1261 if (nspace_table) {
1262 *klass = g_hash_table_lookup (nspace_table, name);
1263 if (*klass) {
1264 mono_aot_unlock ();
1265 return TRUE;
1269 table_size = aot_module->class_name_table [0];
1270 table = aot_module->class_name_table + 1;
1272 if (name_space [0] == '\0')
1273 full_name = g_strdup_printf ("%s", name);
1274 else {
1275 if (strlen (name_space) + strlen (name) < 1000) {
1276 sprintf (full_name_buf, "%s.%s", name_space, name);
1277 full_name = full_name_buf;
1278 } else {
1279 full_name = g_strdup_printf ("%s.%s", name_space, name);
1282 hash = g_str_hash (full_name) % table_size;
1283 if (full_name != full_name_buf)
1284 g_free (full_name);
1286 entry = &table [hash * 2];
1288 if (entry [0] != 0) {
1289 t = &image->tables [MONO_TABLE_TYPEDEF];
1291 while (TRUE) {
1292 guint32 index = entry [0];
1293 guint32 next = entry [1];
1294 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1296 name_table_accesses ++;
1298 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1300 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1301 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1303 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1304 mono_aot_unlock ();
1305 *klass = mono_class_get (image, token);
1307 /* Add to cache */
1308 if (*klass) {
1309 mono_aot_lock ();
1310 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1311 if (!nspace_table) {
1312 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1313 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1315 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1316 mono_aot_unlock ();
1318 return TRUE;
1321 if (next != 0) {
1322 entry = &table [next * 2];
1323 } else {
1324 break;
1329 mono_aot_unlock ();
1331 return TRUE;
1334 static MonoJitInfo*
1335 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1336 MonoMethod *method, guint8* ex_info, guint8 *code)
1338 int i, buf_len;
1339 MonoJitInfo *jinfo;
1340 guint code_len, used_int_regs;
1341 gboolean has_generic_jit_info;
1342 guint8 *p;
1343 MonoMethodHeader *header;
1344 int generic_info_size;
1346 header = mono_method_get_header (method);
1348 /* Load the method info from the AOT file */
1350 p = ex_info;
1351 code_len = decode_value (p, &p);
1352 used_int_regs = decode_value (p, &p);
1353 has_generic_jit_info = decode_value (p, &p);
1354 if (has_generic_jit_info)
1355 generic_info_size = sizeof (MonoGenericJitInfo);
1356 else
1357 generic_info_size = 0;
1359 /* Exception table */
1360 if (header && header->num_clauses) {
1361 jinfo =
1362 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1363 jinfo->num_clauses = header->num_clauses;
1365 for (i = 0; i < header->num_clauses; ++i) {
1366 MonoExceptionClause *ec = &header->clauses [i];
1367 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1369 ei->flags = ec->flags;
1370 ei->exvar_offset = decode_value (p, &p);
1372 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1373 ei->data.filter = code + decode_value (p, &p);
1374 else
1375 ei->data.catch_class = ec->data.catch_class;
1377 ei->try_start = code + decode_value (p, &p);
1378 ei->try_end = code + decode_value (p, &p);
1379 ei->handler_start = code + decode_value (p, &p);
1382 else
1383 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1385 jinfo->code_size = code_len;
1386 jinfo->used_regs = used_int_regs;
1387 jinfo->method = method;
1388 jinfo->code_start = code;
1389 jinfo->domain_neutral = 0;
1391 if (has_generic_jit_info) {
1392 MonoGenericJitInfo *gi;
1394 jinfo->has_generic_jit_info = 1;
1396 gi = mono_jit_info_get_generic_jit_info (jinfo);
1397 g_assert (gi);
1399 gi->has_this = decode_value (p, &p);
1400 gi->this_reg = decode_value (p, &p);
1401 gi->this_offset = decode_value (p, &p);
1403 /* This currently contains no data */
1404 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1406 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1409 /* Load debug info */
1410 buf_len = decode_value (p, &p);
1411 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1413 return jinfo;
1416 MonoJitInfo *
1417 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1420 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1421 int page_index, method_index, table_len, is_wrapper;
1422 guint32 token;
1423 MonoAotModule *amodule = image->aot_module;
1424 MonoMethod *method;
1425 MonoJitInfo *jinfo;
1426 guint8 *code, *ex_info, *p;
1427 guint32 *table, *ptr;
1428 gboolean found;
1430 if (!amodule)
1431 return NULL;
1433 if (domain != mono_get_root_domain ())
1434 /* FIXME: */
1435 return NULL;
1437 offset = (guint8*)addr - amodule->code;
1439 /* First search through the index */
1440 ptr = amodule->method_order;
1441 last_offset = 0;
1442 page_index = 0;
1443 found = FALSE;
1445 if (*ptr == 0xffffff)
1446 return NULL;
1447 ptr ++;
1449 while (*ptr != 0xffffff) {
1450 guint32 method_index = ptr [0];
1451 new_offset = amodule->code_offsets [method_index];
1453 if (offset >= last_offset && offset < new_offset) {
1454 found = TRUE;
1455 break;
1458 ptr ++;
1459 last_offset = new_offset;
1460 page_index ++;
1463 /* Skip rest of index */
1464 while (*ptr != 0xffffff)
1465 ptr ++;
1466 ptr ++;
1468 table = ptr;
1469 table_len = amodule->method_order_end - table;
1471 g_assert (table <= amodule->method_order_end);
1473 if (found) {
1474 left = (page_index * 1024);
1475 right = left + 1024;
1477 if (right > table_len)
1478 right = table_len;
1480 offset1 = amodule->code_offsets [table [left]];
1481 g_assert (offset1 <= offset);
1483 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1485 else {
1486 //printf ("Not found in index: 0x%x\n", offset);
1487 left = 0;
1488 right = table_len;
1491 /* Binary search inside the method_order table to find the method */
1492 while (TRUE) {
1493 pos = (left + right) / 2;
1495 g_assert (table + pos <= amodule->method_order_end);
1497 //printf ("Pos: %5d < %5d < %5d Offset: 0x%05x < 0x%05x < 0x%05x\n", left, pos, right, amodule->code_offsets [table [left]], offset, amodule->code_offsets [table [right]]);
1499 offset1 = amodule->code_offsets [table [pos]];
1500 if (table + pos + 1 >= amodule->method_order_end)
1501 offset2 = amodule->code_end - amodule->code;
1502 else
1503 offset2 = amodule->code_offsets [table [pos + 1]];
1505 if (offset < offset1)
1506 right = pos;
1507 else if (offset >= offset2)
1508 left = pos + 1;
1509 else
1510 break;
1513 method_index = table [pos];
1515 /* Might be a wrapper/extra method */
1516 if (amodule->extra_methods) {
1517 mono_aot_lock ();
1518 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1519 mono_aot_unlock ();
1520 } else {
1521 method = NULL;
1524 if (!method) {
1525 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1527 * This is hit for extra methods which are called directly, so they are
1528 * not in amodule->extra_methods.
1530 table_len = amodule->extra_method_info_offsets [0];
1531 table = amodule->extra_method_info_offsets + 1;
1532 left = 0;
1533 right = table_len;
1534 pos = 0;
1536 /* Binary search */
1537 while (TRUE) {
1538 pos = ((left + right) / 2);
1540 g_assert (pos < table_len);
1542 if (table [pos * 2] < method_index)
1543 left = pos + 1;
1544 else if (table [pos * 2] > method_index)
1545 right = pos;
1546 else
1547 break;
1550 p = amodule->extra_method_info + table [(pos * 2) + 1];
1551 is_wrapper = decode_value (p, &p);
1552 g_assert (!is_wrapper);
1553 method = decode_method_ref_2 (amodule, p, &p);
1554 g_assert (method);
1555 } else {
1556 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1557 method = mono_get_method (image, token, NULL);
1561 /* FIXME: */
1562 g_assert (method);
1564 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1566 code = &amodule->code [amodule->code_offsets [method_index]];
1567 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1569 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1571 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1572 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1574 /* Add it to the normal JitInfo tables */
1575 mono_jit_info_table_add (domain, jinfo);
1577 return jinfo;
1580 /* Keep it in sync with the version in aot-compiler.c */
1581 static inline gboolean
1582 is_shared_got_patch (MonoJumpInfo *patch_info)
1584 switch (patch_info->type) {
1585 case MONO_PATCH_INFO_VTABLE:
1586 case MONO_PATCH_INFO_CLASS:
1587 case MONO_PATCH_INFO_IID:
1588 case MONO_PATCH_INFO_ADJUSTED_IID:
1589 case MONO_PATCH_INFO_FIELD:
1590 case MONO_PATCH_INFO_SFLDA:
1591 case MONO_PATCH_INFO_DECLSEC:
1592 case MONO_PATCH_INFO_LDTOKEN:
1593 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1594 case MONO_PATCH_INFO_RVA:
1595 case MONO_PATCH_INFO_METHODCONST:
1596 return TRUE;
1597 default:
1598 return FALSE;
1602 static gboolean
1603 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1605 guint8 *p = buf;
1606 gpointer *table;
1607 MonoImage *image;
1608 int i;
1610 switch (ji->type) {
1611 case MONO_PATCH_INFO_METHOD:
1612 case MONO_PATCH_INFO_METHOD_JUMP:
1613 case MONO_PATCH_INFO_ICALL_ADDR:
1614 case MONO_PATCH_INFO_METHOD_RGCTX: {
1615 guint32 token;
1616 MonoMethod *method;
1617 gboolean no_aot_trampoline;
1619 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1620 if (!image)
1621 goto cleanup;
1623 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1624 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1625 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1626 ji->type = MONO_PATCH_INFO_ABS;
1628 else {
1629 if (method)
1630 ji->data.method = method;
1631 else
1632 ji->data.method = mono_get_method (image, token, NULL);
1633 g_assert (ji->data.method);
1634 mono_class_init (ji->data.method->klass);
1636 #else
1637 ji->data.method = mono_get_method (image, token, NULL);
1638 g_assert (ji->data.method);
1639 mono_class_init (ji->data.method->klass);
1640 #endif
1642 break;
1644 case MONO_PATCH_INFO_INTERNAL_METHOD:
1645 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1646 guint32 len = decode_value (p, &p);
1648 ji->data.name = (char*)p;
1649 p += len + 1;
1650 break;
1652 case MONO_PATCH_INFO_METHODCONST:
1653 /* Shared */
1654 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1655 if (!ji->data.method)
1656 goto cleanup;
1657 break;
1658 case MONO_PATCH_INFO_VTABLE:
1659 case MONO_PATCH_INFO_CLASS:
1660 case MONO_PATCH_INFO_IID:
1661 case MONO_PATCH_INFO_ADJUSTED_IID:
1662 /* Shared */
1663 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1664 if (!ji->data.klass)
1665 goto cleanup;
1666 break;
1667 case MONO_PATCH_INFO_CLASS_INIT:
1668 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1669 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1670 if (!ji->data.klass)
1671 goto cleanup;
1672 break;
1673 case MONO_PATCH_INFO_IMAGE:
1674 ji->data.image = load_image (aot_module, decode_value (p, &p));
1675 if (!ji->data.image)
1676 goto cleanup;
1677 break;
1678 case MONO_PATCH_INFO_FIELD:
1679 case MONO_PATCH_INFO_SFLDA:
1680 /* Shared */
1681 ji->data.field = decode_field_info (aot_module, p, &p);
1682 if (!ji->data.field)
1683 goto cleanup;
1684 break;
1685 case MONO_PATCH_INFO_SWITCH:
1686 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1687 ji->data.table->table_size = decode_value (p, &p);
1688 table = g_new (gpointer, ji->data.table->table_size);
1689 ji->data.table->table = (MonoBasicBlock**)table;
1690 for (i = 0; i < ji->data.table->table_size; i++)
1691 table [i] = (gpointer)(gssize)decode_value (p, &p);
1692 break;
1693 case MONO_PATCH_INFO_R4: {
1694 guint32 val;
1696 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1697 val = decode_value (p, &p);
1698 *(float*)ji->data.target = *(float*)&val;
1699 break;
1701 case MONO_PATCH_INFO_R8: {
1702 guint32 val [2];
1704 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1706 val [0] = decode_value (p, &p);
1707 val [1] = decode_value (p, &p);
1708 *(double*)ji->data.target = *(double*)val;
1709 break;
1711 case MONO_PATCH_INFO_LDSTR:
1712 image = load_image (aot_module, decode_value (p, &p));
1713 if (!image)
1714 goto cleanup;
1715 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1716 break;
1717 case MONO_PATCH_INFO_RVA:
1718 case MONO_PATCH_INFO_DECLSEC:
1719 case MONO_PATCH_INFO_LDTOKEN:
1720 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1721 /* Shared */
1722 image = load_image (aot_module, decode_value (p, &p));
1723 if (!image)
1724 goto cleanup;
1725 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1727 ji->data.token->has_context = decode_value (p, &p);
1728 if (ji->data.token->has_context) {
1729 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1730 if (!res)
1731 goto cleanup;
1733 break;
1734 case MONO_PATCH_INFO_EXC_NAME:
1735 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1736 if (!ji->data.klass)
1737 goto cleanup;
1738 ji->data.name = ji->data.klass->name;
1739 break;
1740 case MONO_PATCH_INFO_METHOD_REL:
1741 ji->data.offset = decode_value (p, &p);
1742 break;
1743 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1744 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1745 break;
1746 case MONO_PATCH_INFO_RGCTX_FETCH: {
1747 gboolean res;
1748 MonoJumpInfoRgctxEntry *entry;
1750 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1751 entry->method = decode_method_ref_2 (aot_module, p, &p);
1752 entry->in_mrgctx = decode_value (p, &p);
1753 entry->info_type = decode_value (p, &p);
1754 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1755 entry->data->type = decode_value (p, &p);
1757 res = decode_patch (aot_module, mp, entry->data, p, &p);
1758 if (!res)
1759 goto cleanup;
1760 ji->data.rgctx_entry = entry;
1761 break;
1763 default:
1764 g_warning ("unhandled type %d", ji->type);
1765 g_assert_not_reached ();
1768 *endbuf = p;
1770 return TRUE;
1772 cleanup:
1773 return FALSE;
1776 static gboolean
1777 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1779 guint8 *p = buf;
1780 guint8 *shared_p;
1781 gboolean res;
1783 if (is_shared_got_patch (ji)) {
1784 *got_offset = decode_value (p, &p);
1786 if (aot_module->got [*got_offset]) {
1787 /* Already loaded */
1788 //printf ("HIT!\n");
1789 } else {
1790 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1792 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1793 if (!res)
1794 return FALSE;
1796 } else {
1797 res = decode_patch (aot_module, mp, ji, p, &p);
1798 if (!res)
1799 return FALSE;
1802 *endbuf = p;
1803 return TRUE;
1806 static MonoJumpInfo*
1807 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1808 guint32 got_index, guint32 **got_slots,
1809 guint8 *buf, guint8 **endbuf)
1811 MonoJumpInfo *patches;
1812 MonoJumpInfo *patch_info = NULL;
1813 int pindex;
1814 guint32 last_offset;
1815 guint8 *p;
1817 p = buf;
1819 /* First load the type + offset table */
1820 last_offset = 0;
1821 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1823 for (pindex = 0; pindex < n_patches; ++pindex) {
1824 MonoJumpInfo *ji = &patches [pindex];
1826 ji->type = *p;
1827 p ++;
1829 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1830 ji->next = patch_info;
1831 patch_info = ji;
1834 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1835 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1837 /* Then load the other data */
1838 for (pindex = 0; pindex < n_patches; ++pindex) {
1839 MonoJumpInfo *ji = &patches [pindex];
1841 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1842 goto cleanup;
1844 if ((*got_slots) [pindex] == 0xffffffff)
1845 (*got_slots) [pindex] = got_index ++;
1848 *endbuf = p;
1849 return patches;
1851 cleanup:
1852 g_free (*got_slots);
1853 *got_slots = NULL;
1855 return NULL;
1858 static void
1859 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1862 * Jump addresses cannot be patched by the trampoline code since it
1863 * does not have access to the caller's address. Instead, we collect
1864 * the addresses of the GOT slots pointing to a method, and patch
1865 * them after the method has been compiled.
1867 MonoJitDomainInfo *info = domain_jit_info (domain);
1868 GSList *list;
1870 mono_domain_lock (domain);
1871 if (!info->jump_target_got_slot_hash)
1872 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1873 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1874 list = g_slist_prepend (list, got_slot);
1875 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1876 mono_domain_unlock (domain);
1880 * load_method:
1882 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1883 * pointer to the native code of the method, or NULL if not found.
1884 * METHOD might not be set if the caller only has the image/token info.
1886 static gpointer
1887 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1889 MonoClass *klass;
1890 MonoJumpInfo *patch_info = NULL;
1891 gboolean from_plt = method == NULL;
1892 MonoMemPool *mp;
1893 int i, pindex, got_index = 0, n_patches, used_strings;
1894 gboolean non_got_patches, keep_patches = TRUE;
1895 guint8 *p, *ex_info;
1896 MonoJitInfo *jinfo = NULL;
1897 guint8 *code, *info;
1899 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1900 return NULL;
1902 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1903 /* Non shared AOT code can't be used in other appdomains */
1904 return NULL;
1906 if (aot_module->out_of_date)
1907 return NULL;
1909 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1910 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1911 char *full_name;
1913 if (!method)
1914 method = mono_get_method (image, token, NULL);
1915 full_name = mono_method_full_name (method, TRUE);
1916 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1917 g_free (full_name);
1919 return NULL;
1922 code = &aot_module->code [aot_module->code_offsets [method_index]];
1923 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1925 mono_aot_lock ();
1926 if (!aot_module->methods_loaded)
1927 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1928 mono_aot_unlock ();
1930 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1931 return code;
1933 if (mono_last_aot_method != -1) {
1934 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1935 return NULL;
1936 else
1937 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
1938 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1941 p = info;
1943 if (method) {
1944 klass = method->klass;
1945 decode_klass_ref (aot_module, p, &p);
1946 } else {
1947 klass = decode_klass_ref (aot_module, p, &p);
1950 if (!use_loaded_code) {
1951 guint8 *code2;
1953 if (!jinfo) {
1954 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
1955 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1958 mono_domain_lock (domain);
1959 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1960 mono_domain_unlock (domain);
1961 memcpy (code2, code, jinfo->code_size);
1962 mono_arch_flush_icache (code2, jinfo->code_size);
1963 code = code2;
1966 if (aot_module->opts & MONO_OPT_SHARED)
1967 used_strings = decode_value (p, &p);
1968 else
1969 used_strings = 0;
1971 for (i = 0; i < used_strings; i++) {
1972 guint token = decode_value (p, &p);
1973 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
1976 if (aot_module->opts & MONO_OPT_SHARED)
1977 keep_patches = FALSE;
1979 n_patches = decode_value (p, &p);
1981 keep_patches = FALSE;
1983 if (n_patches) {
1984 MonoJumpInfo *patches;
1985 guint32 *got_slots;
1987 if (keep_patches)
1988 mp = domain->mp;
1989 else
1990 mp = mono_mempool_new ();
1992 got_index = decode_value (p, &p);
1994 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1995 if (patches == NULL)
1996 goto cleanup;
1998 non_got_patches = FALSE;
1999 for (pindex = 0; pindex < n_patches; ++pindex) {
2000 MonoJumpInfo *ji = &patches [pindex];
2002 if (is_got_patch (ji->type)) {
2003 if (!aot_module->got [got_slots [pindex]]) {
2004 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2005 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2006 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2008 ji->type = MONO_PATCH_INFO_NONE;
2010 else
2011 non_got_patches = TRUE;
2013 if (non_got_patches) {
2014 if (!jinfo) {
2015 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
2016 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2019 mono_arch_flush_icache (code, jinfo->code_size);
2020 make_writable (code, jinfo->code_size);
2021 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
2024 g_free (got_slots);
2026 if (!keep_patches)
2027 mono_mempool_destroy (mp);
2030 mono_aot_lock ();
2032 mono_jit_stats.methods_aot++;
2034 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2035 char *full_name;
2037 if (!method)
2038 method = mono_get_method (image, token, NULL);
2040 full_name = mono_method_full_name (method, TRUE);
2042 if (!jinfo) {
2043 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2044 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2047 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info);
2048 g_free (full_name);
2051 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2053 init_plt (aot_module);
2055 if (method && method->wrapper_type)
2056 g_hash_table_insert (aot_module->method_to_code, method, code);
2058 mono_aot_unlock ();
2060 if (from_plt && klass)
2061 mono_runtime_class_init (mono_class_vtable (domain, klass));
2063 return code;
2065 cleanup:
2066 /* FIXME: The space in domain->mp is wasted */
2067 if (aot_module->opts & MONO_OPT_SHARED)
2068 /* No need to cache patches */
2069 mono_mempool_destroy (mp);
2071 if (jinfo)
2072 g_free (jinfo);
2074 return NULL;
2077 static guint32
2078 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2080 guint32 table_size, entry_size, hash;
2081 guint32 *table, *entry;
2082 char *full_name = NULL;
2084 if (!amodule)
2085 return 0xffffff;
2087 table_size = amodule->extra_method_table [0];
2088 table = amodule->extra_method_table + 1;
2089 entry_size = 3;
2091 if (method->wrapper_type) {
2092 /* FIXME: This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
2093 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
2094 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2095 full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
2096 g_free (tmpsig);
2097 } else {
2098 full_name = mono_method_full_name (method, TRUE);
2102 if (method->wrapper_type)
2103 hash = g_str_hash (method->name) % table_size;
2104 else
2105 hash = 0 % table_size;
2107 entry = &table [hash * entry_size];
2109 if (entry [0] != 0) {
2110 while (TRUE) {
2111 guint32 key = entry [0];
2112 guint32 value = entry [1];
2113 guint32 next = entry [entry_size - 1];
2114 MonoMethod *m;
2115 guint8 *p;
2116 int is_wrapper;
2118 // FIXME: Avoid fully decoding the method ref
2119 p = amodule->extra_method_info + key;
2120 is_wrapper = decode_value (p, &p);
2121 if (method->wrapper_type && is_wrapper) {
2122 if (!strcmp (full_name, (char*)p))
2123 return value;
2124 } else {
2125 m = decode_method_ref_2 (amodule, p, &p);
2126 if (m == method)
2127 return value;
2130 if (next != 0) {
2131 entry = &table [next * entry_size];
2132 } else {
2133 break;
2138 return 0xffffff;
2141 static void
2142 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2144 g_ptr_array_add ((GPtrArray*)user_data, value);
2148 * find_extra_method:
2150 * Try finding METHOD in the extra_method table in all AOT images.
2151 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2152 * module where the method was found.
2154 static guint32
2155 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2157 guint32 index;
2158 GPtrArray *modules;
2159 int i;
2161 /* Try the method's module first */
2162 *out_amodule = method->klass->image->aot_module;
2163 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2164 if (index != 0xffffff)
2165 return index;
2168 * Try all other modules.
2169 * This is needed because generic instances klass->image points to the image
2170 * containing the generic definition, but the native code is generated to the
2171 * AOT image which contains the reference.
2174 /* Make a copy to avoid doing the search inside the aot lock */
2175 modules = g_ptr_array_new ();
2176 mono_aot_lock ();
2177 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2178 mono_aot_unlock ();
2180 index = 0xffffff;
2181 for (i = 0; i < modules->len; ++i) {
2182 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2184 if (amodule != method->klass->image->aot_module)
2185 index = find_extra_method_in_amodule (amodule, method);
2186 if (index != 0xffffff) {
2187 *out_amodule = amodule;
2188 break;
2192 g_ptr_array_free (modules, TRUE);
2194 return index;
2197 gpointer
2198 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2200 MonoClass *klass = method->klass;
2201 guint32 method_index;
2202 MonoAotModule *amodule = klass->image->aot_module;
2203 guint8 *code;
2205 if (!amodule)
2206 return NULL;
2208 if (amodule->out_of_date)
2209 return NULL;
2211 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2212 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2213 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2214 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2215 return NULL;
2217 g_assert (klass->inited);
2219 /* Find method index */
2220 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2221 method = mono_method_get_declaring_generic_method (method);
2222 method_index = mono_metadata_token_index (method->token) - 1;
2223 } else if (method->is_inflated || !method->token) {
2224 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2225 mono_aot_lock ();
2226 code = g_hash_table_lookup (amodule->method_to_code, method);
2227 mono_aot_unlock ();
2228 if (code)
2229 return code;
2231 method_index = find_extra_method (method, &amodule);
2232 if (method_index == 0xffffff) {
2233 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2234 char *full_name;
2236 full_name = mono_method_full_name (method, TRUE);
2237 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2238 g_free (full_name);
2240 return NULL;
2243 if (method_index == 0xffffff)
2244 return NULL;
2246 /* Needed by find_jit_info */
2247 mono_aot_lock ();
2248 if (!amodule->extra_methods)
2249 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2250 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2251 mono_aot_unlock ();
2252 } else {
2253 /* Common case */
2254 method_index = mono_metadata_token_index (method->token) - 1;
2257 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2261 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2262 * method.
2264 gpointer
2265 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2267 MonoAotModule *aot_module = image->aot_module;
2268 int method_index;
2270 if (!aot_module)
2271 return NULL;
2273 method_index = mono_metadata_token_index (token) - 1;
2275 return load_method (domain, aot_module, image, NULL, token, method_index);
2278 typedef struct {
2279 guint8 *addr;
2280 gboolean res;
2281 } IsGotEntryUserData;
2283 static void
2284 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2286 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2287 MonoAotModule *aot_module = (MonoAotModule*)value;
2289 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2290 data->res = TRUE;
2293 gboolean
2294 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2296 IsGotEntryUserData user_data;
2298 if (!aot_modules)
2299 return FALSE;
2301 user_data.addr = addr;
2302 user_data.res = FALSE;
2303 mono_aot_lock ();
2304 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2305 mono_aot_unlock ();
2307 return user_data.res;
2310 typedef struct {
2311 guint8 *addr;
2312 MonoAotModule *module;
2313 } FindAotModuleUserData;
2315 static void
2316 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2318 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2319 MonoAotModule *aot_module = (MonoAotModule*)value;
2321 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2322 data->module = aot_module;
2325 static inline MonoAotModule*
2326 find_aot_module (guint8 *code)
2328 FindAotModuleUserData user_data;
2330 if (!aot_modules)
2331 return NULL;
2333 /* Reading these need no locking */
2334 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2335 return NULL;
2337 user_data.addr = code;
2338 user_data.module = NULL;
2340 mono_aot_lock ();
2341 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2342 mono_aot_unlock ();
2344 return user_data.module;
2348 * mono_aot_set_make_unreadable:
2350 * Set whenever to make all mmaped memory unreadable. In conjuction with a
2351 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
2353 void
2354 mono_aot_set_make_unreadable (gboolean unreadable)
2356 make_unreadable = unreadable;
2359 typedef struct {
2360 MonoAotModule *module;
2361 guint8 *ptr;
2362 } FindMapUserData;
2364 static void
2365 find_map (gpointer key, gpointer value, gpointer user_data)
2367 MonoAotModule *module = (MonoAotModule*)value;
2368 FindMapUserData *data = (FindMapUserData*)user_data;
2370 if (!data->module)
2371 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
2372 data->module = module;
2375 static MonoAotModule*
2376 find_module_for_addr (void *ptr)
2378 FindMapUserData data;
2380 if (!make_unreadable)
2381 return NULL;
2383 data.module = NULL;
2384 data.ptr = (guint8*)ptr;
2386 mono_aot_lock ();
2387 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
2388 mono_aot_unlock ();
2390 return data.module;
2394 * mono_aot_is_pagefault:
2396 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
2397 * within memory allocated by this module.
2399 gboolean
2400 mono_aot_is_pagefault (void *ptr)
2402 if (!make_unreadable)
2403 return FALSE;
2405 return find_module_for_addr (ptr) != NULL;
2409 * mono_aot_handle_pagefault:
2411 * Handle a pagefault caused by an unreadable page by making it readable again.
2413 void
2414 mono_aot_handle_pagefault (void *ptr)
2416 #ifndef PLATFORM_WIN32
2417 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
2418 int res;
2420 mono_aot_lock ();
2421 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
2422 g_assert (res == 0);
2424 n_pagefaults ++;
2425 mono_aot_unlock ();
2427 #if 0
2429 void *array [256];
2430 char **names;
2431 int i, size;
2433 printf ("\nNative stacktrace:\n\n");
2435 size = backtrace (array, 256);
2436 names = backtrace_symbols (array, size);
2437 for (i =0; i < size; ++i) {
2438 printf ("\t%s\n", names [i]);
2440 free (names);
2442 #endif
2444 #endif
2448 * mono_aot_plt_resolve:
2450 * This function is called by the entries in the PLT to resolve the actual method that
2451 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2453 gpointer
2454 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2456 #ifdef MONO_ARCH_AOT_SUPPORTED
2457 guint8 *p, *target, *plt_entry;
2458 MonoJumpInfo ji;
2459 MonoAotModule *module = (MonoAotModule*)aot_module;
2460 gboolean res;
2461 MonoMemPool *mp;
2463 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2465 p = &module->plt_info [plt_info_offset];
2467 ji.type = decode_value (p, &p);
2469 mp = mono_mempool_new_size (512);
2470 res = decode_patch (module, mp, &ji, p, &p);
2471 // FIXME: Error handling (how ?)
2472 g_assert (res);
2474 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2476 mono_mempool_destroy (mp);
2478 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2479 plt_entry = mono_aot_get_plt_entry (code);
2480 g_assert (plt_entry);
2481 mono_arch_patch_plt_entry (plt_entry, target);
2483 return target;
2484 #else
2485 g_assert_not_reached ();
2486 return NULL;
2487 #endif
2491 * init_plt:
2493 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2494 * method in the module is loaded to avoid committing memory by writing to it.
2495 * LOCKING: Assumes the AOT lock is held.
2497 static void
2498 init_plt (MonoAotModule *info)
2500 #ifdef MONO_ARCH_AOT_SUPPORTED
2501 #ifdef __i386__
2502 guint8 *buf = info->plt;
2503 #elif defined(__x86_64__)
2504 int i, n_entries;
2505 #elif defined(__arm__)
2506 int i, n_entries;
2507 #endif
2508 gpointer tramp;
2510 if (info->plt_inited)
2511 return;
2513 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2515 #ifdef __i386__
2516 /* Initialize the first PLT entry */
2517 make_writable (info->plt, info->plt_end - info->plt);
2518 x86_jump_code (buf, tramp);
2519 #elif defined(__x86_64__) || defined(__arm__)
2521 * Initialize the entries in the plt_jump_table to point to the default targets.
2523 n_entries = info->plt_jump_table_size / sizeof (gpointer);
2525 /* The first entry points to the AOT trampoline */
2526 ((gpointer*)info->plt_jump_table)[0] = tramp;
2527 for (i = 1; i < n_entries; ++i)
2528 /* All the default entries point to the first entry */
2529 ((gpointer*)info->plt_jump_table)[i] = info->plt;
2530 #else
2531 g_assert_not_reached ();
2532 #endif
2534 info->plt_inited = TRUE;
2535 #endif
2539 * mono_aot_get_plt_entry:
2541 * Return the address of the PLT entry called by the code at CODE if exists.
2543 guint8*
2544 mono_aot_get_plt_entry (guint8 *code)
2546 MonoAotModule *aot_module = find_aot_module (code);
2547 #if defined(__arm__)
2548 guint32 ins;
2549 #endif
2551 if (!aot_module)
2552 return NULL;
2554 #if defined(__i386__) || defined(__x86_64__)
2555 if (code [-5] == 0xe8) {
2556 guint32 disp = *(guint32*)(code - 4);
2557 guint8 *target = code + disp;
2559 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2560 return target;
2562 #elif defined(__arm__)
2563 ins = ((guint32*)(gpointer)code) [-1];
2565 /* Should be a 'bl' */
2566 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2567 gint32 disp = ((gint32)ins) & 0xffffff;
2568 guint8 *target = code - 4 + 8 + (disp * 4);
2570 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2571 return target;
2573 #else
2574 g_assert_not_reached ();
2575 #endif
2577 return NULL;
2581 * mono_aot_get_plt_info_offset:
2583 * Return the PLT info offset belonging to the plt entry called by CODE.
2585 guint32
2586 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2588 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2590 g_assert (plt_entry);
2592 /* The offset is embedded inside the code after the plt entry */
2593 #if defined(__i386__)
2594 return *(guint32*)(plt_entry + 5);
2595 #elif defined(__x86_64__)
2596 return *(guint32*)(plt_entry + 6);
2597 #elif defined(__arm__)
2598 /* The offset is stored as the 5th word of the plt entry */
2599 return ((guint32*)plt_entry) [4];
2600 #else
2601 g_assert_not_reached ();
2602 return 0;
2603 #endif
2606 static gpointer
2607 load_named_code (MonoAotModule *amodule, const char *name)
2609 char *symbol;
2610 guint8 *p;
2611 int n_patches, got_index, pindex;
2612 MonoMemPool *mp;
2613 gpointer code;
2615 /* Load the code */
2617 symbol = g_strdup_printf ("%s", name);
2618 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2619 g_free (symbol);
2620 if (!code)
2621 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2623 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2625 /* Load info */
2627 symbol = g_strdup_printf ("%s_p", name);
2628 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2629 g_free (symbol);
2630 if (!p)
2631 /* Nothing to patch */
2632 return code;
2634 /* Similar to mono_aot_load_method () */
2636 n_patches = decode_value (p, &p);
2638 if (n_patches) {
2639 MonoJumpInfo *patches;
2640 guint32 *got_slots;
2642 mp = mono_mempool_new ();
2644 got_index = decode_value (p, &p);
2646 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2647 g_assert (patches);
2649 for (pindex = 0; pindex < n_patches; ++pindex) {
2650 MonoJumpInfo *ji = &patches [pindex];
2651 gpointer target;
2654 * When this code is executed, the runtime may not yet initalized, so
2655 * resolve the patch info by hand.
2657 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2658 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2659 target = mono_get_lmf_addr;
2660 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2661 target = mono_thread_force_interruption_checkpoint;
2662 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2663 target = mono_exception_from_token;
2664 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2665 target = mono_get_throw_exception ();
2666 #ifdef __x86_64__
2667 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2668 target = mono_amd64_throw_exception;
2669 #endif
2670 #ifdef __arm__
2671 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2672 target = mono_arm_throw_exception;
2673 #endif
2674 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2675 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2676 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2677 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2678 guint32 slot = atoi (ji->data.name + strlen ("specific_trampoline_lazy_fetch_"));
2679 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2680 } else {
2681 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2682 g_assert_not_reached ();
2683 target = NULL;
2685 } else {
2686 /* Hopefully the code doesn't have patches which need method or
2687 * domain to be set.
2689 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2692 amodule->got [got_slots [pindex]] = target;
2695 g_free (got_slots);
2697 mono_mempool_destroy (mp);
2700 return code;
2704 * Return the piece of code identified by NAME from the mscorlib AOT file.
2706 gpointer
2707 mono_aot_get_named_code (const char *name)
2709 MonoImage *image;
2710 MonoAotModule *amodule;
2712 image = mono_defaults.corlib;
2713 g_assert (image);
2715 amodule = image->aot_module;
2716 g_assert (amodule);
2718 return load_named_code (amodule, name);
2722 * Return a specific trampoline from the AOT file.
2724 gpointer
2725 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2727 MonoAotModule *amodule;
2728 int index, tramp_size;
2729 guint8 *code, *tramp;
2730 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2732 /* Currently, we keep all trampolines in the mscorlib AOT image */
2733 image = mono_defaults.corlib;
2734 g_assert (image);
2736 mono_aot_lock ();
2738 amodule = image->aot_module;
2739 g_assert (amodule);
2741 if (amodule->trampoline_index == amodule->num_trampolines)
2742 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2744 index = amodule->trampoline_index ++;
2746 mono_aot_unlock ();
2748 if (!generic_trampolines [tramp_type]) {
2749 char *symbol;
2751 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2752 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2753 g_free (symbol);
2756 tramp = generic_trampolines [tramp_type];
2757 g_assert (tramp);
2759 amodule->got [amodule->first_trampoline_got_offset + (index *2)] = tramp;
2760 amodule->got [amodule->first_trampoline_got_offset + (index *2) + 1] = arg1;
2762 #ifdef __x86_64__
2763 tramp_size = 16;
2764 #elif defined(__arm__)
2765 tramp_size = 28;
2766 #else
2767 tramp_size = -1;
2768 g_assert_not_reached ();
2769 #endif
2771 code = amodule->trampolines + (index * tramp_size);
2772 if (code_len)
2773 *code_len = tramp_size;
2775 return code;
2778 gpointer
2779 mono_aot_get_unbox_trampoline (MonoMethod *method)
2781 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2782 MonoAotModule *amodule;
2783 char *symbol;
2784 gpointer code;
2786 amodule = method->klass->image->aot_module;
2787 g_assert (amodule);
2789 symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2790 code = load_named_code (amodule, symbol);
2791 g_free (symbol);
2792 return code;
2795 gpointer
2796 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2798 char *symbol;
2799 gpointer code;
2801 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2802 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2803 g_free (symbol);
2804 return code;
2808 * mono_aot_get_n_pagefaults:
2810 * Return the number of times handle_pagefault is called.
2812 guint32
2813 mono_aot_get_n_pagefaults (void)
2815 return n_pagefaults;
2818 #else
2819 /* AOT disabled */
2821 void
2822 mono_aot_init (void)
2826 gpointer
2827 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2829 return NULL;
2832 gboolean
2833 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2835 return FALSE;
2838 gboolean
2839 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2841 return FALSE;
2844 gboolean
2845 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2847 return FALSE;
2850 MonoJitInfo *
2851 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2853 return NULL;
2856 gpointer
2857 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2859 return NULL;
2862 gboolean
2863 mono_aot_is_pagefault (void *ptr)
2865 return FALSE;
2868 void
2869 mono_aot_set_make_unreadable (gboolean unreadable)
2873 guint32
2874 mono_aot_get_n_pagefaults (void)
2876 return 0;
2879 void
2880 mono_aot_handle_pagefault (void *ptr)
2884 guint8*
2885 mono_aot_get_plt_entry (guint8 *code)
2887 return NULL;
2890 gpointer
2891 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2893 return NULL;
2896 gpointer
2897 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2899 return NULL;
2902 gpointer
2903 mono_aot_create_specific_trampolines (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2905 g_assert_not_reached ();
2906 return NULL;
2909 gpointer
2910 mono_aot_get_named_code (char *name)
2912 g_assert_not_reached ();
2913 return NULL;
2916 gpointer
2917 mono_aot_get_unbox_trampoline (MonoMethod *method)
2919 g_assert_not_reached ();
2920 return NULL;
2923 #endif