diff --git a/Make.config b/Make.config index 24db31d67170..2a832c8fb385 100644 --- a/Make.config +++ b/Make.config @@ -682,6 +682,7 @@ DOTNET_PLATFORMS= ifdef INCLUDE_IOS DOTNET_PLATFORMS+=iOS DOTNET_IOS_BITNESSES+=64 +DOTNET_NATIVEAOT_PLATFORMS+=iOS # 32-bit architectures ifdef IOS_SUPPORTS_32BIT_ARCHITECTURES diff --git a/mk/rules.mk b/mk/rules.mk index 2d309e3389fa..6910cdda651e 100644 --- a/mk/rules.mk +++ b/mk/rules.mk @@ -297,6 +297,8 @@ $(eval $(call NativeCompilationTemplate,-dotnet,-O2 -DDOTNET)) $(eval $(call NativeCompilationTemplate,-dotnet-debug,-DDEBUG -DDOTNET)) $(eval $(call NativeCompilationTemplate,-dotnet-coreclr,-O2 -DCORECLR_RUNTIME -DDOTNET)) $(eval $(call NativeCompilationTemplate,-dotnet-coreclr-debug,-DDEBUG -DCORECLR_RUNTIME -DDOTNET)) +$(eval $(call NativeCompilationTemplate,-dotnet-nativeaot,-O2 -DCORECLR_RUNTIME -DDOTNET -DNATIVEAOT)) +$(eval $(call NativeCompilationTemplate,-dotnet-nativeaot-debug,-DDEBUG -DCORECLR_RUNTIME -DDOTNET -DNATIVEAOT)) .libs/iphoneos .libs/iphonesimulator .libs/watchos .libs/watchsimulator .libs/tvos .libs/tvsimulator .libs/maccatalyst .libs/mac: $(Q) mkdir -p $@ diff --git a/runtime/Makefile b/runtime/Makefile index 88a03a4e0c92..74ba126c79ef 100644 --- a/runtime/Makefile +++ b/runtime/Makefile @@ -22,7 +22,7 @@ SHIPPED_HEADERS += \ xamarin/monovm-bridge.h \ xamarin/coreclr-bridge.h \ -SHARED_SOURCES += mono-runtime.m bindings.m bindings-generated.m shared.m runtime.m trampolines.m trampolines-invoke.m xamarin-support.m nsstring-localization.m trampolines-varargs.m monovm-bridge.m coreclr-bridge.m +SHARED_SOURCES += mono-runtime.m bindings.m bindings-generated.m shared.m runtime.m trampolines.m trampolines-invoke.m xamarin-support.m nsstring-localization.m trampolines-varargs.m monovm-bridge.m coreclr-bridge.m nativeaot-bridge.m SHARED_I386_SOURCES += trampolines-i386.m trampolines-i386-asm.s trampolines-i386-objc_msgSend.s trampolines-i386-objc_msgSendSuper.s trampolines-i386-objc_msgSend_stret.s trampolines-i386-objc_msgSendSuper_stret.s SHARED_X86_64_SOURCES += trampolines-x86_64.m trampolines-x86_64-asm.s trampolines-x86_64-objc_msgSend.s trampolines-x86_64-objc_msgSendSuper.s trampolines-x86_64-objc_msgSend_stret.s trampolines-x86_64-objc_msgSendSuper_stret.s SHARED_ARM64_SOURCES += trampolines-arm64.m trampolines-arm64-asm.s trampolines-arm64-objc_msgSend.s trampolines-arm64-objc_msgSendSuper.s @@ -454,6 +454,11 @@ MAC_LIBS = \ # This is used when using the CoreCLR runtime instead of Mono. # CORECLR_RUNTIME is defined for these versions of libxamarin. # +# libxamarin-nativeaot.a: +# This is used when using NativeAOT. +# CORECLR_RUNTIME is defined for these versions of libxamarin (because the nativeaot bridge shares *a lot* of code with the coreclr bridge) +# NATIVEAOT is defined for these versions of libxamarin +# ifdef INCLUDE_XAMARIN_LEGACY all-local:: $(TARGETS) @@ -614,6 +619,17 @@ endef $(foreach platform,$(DOTNET_CORECLR_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(eval $(call DotNetCoreClrLibTemplate,$(platform),$(rid))))) +define DotNetNativeAotLibTemplate +DOTNET_TARGETS += \ + $(DOTNET_DESTDIR)/Microsoft.$(1).Runtime.$(2)/runtimes/$(2)/native/libxamarin-dotnet-nativeaot.a \ + $(DOTNET_DESTDIR)/Microsoft.$(1).Runtime.$(2)/runtimes/$(2)/native/libxamarin-dotnet-nativeaot-debug.a \ + $(DOTNET_DESTDIR)/Microsoft.$(1).Runtime.$(2)/runtimes/$(2)/native/libxamarin-dotnet-nativeaot.dylib \ + $(DOTNET_DESTDIR)/Microsoft.$(1).Runtime.$(2)/runtimes/$(2)/native/libxamarin-dotnet-nativeaot-debug.dylib \ + +endef + +$(foreach platform,$(DOTNET_NATIVEAOT_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(eval $(call DotNetNativeAotLibTemplate,$(platform),$(rid))))) + # a few lookup tables, because the data we have is not always in the format we need it DOTNET_iphonesimulator_DYLIB_FLAGS=-lmonosgen-2.0 -licudata -licui18n -licuuc -framework UIKit DOTNET_iphoneos_DYLIB_FLAGS=-lmonosgen-2.0 -licudata -licui18n -licuuc -framework UIKit @@ -704,6 +720,8 @@ $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIM $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibXamarinTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_DEBUG,-dotnet-debug,,.mono))))) $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibXamarinTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_CORECLR,-dotnet-coreclr,_CORECLR))))) $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibXamarinTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_CORECLR_DEBUG,-dotnet-coreclr-debug,_CORECLR))))) +$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibXamarinTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_NATIVEAOT,-dotnet-nativeaot,_NATIVEAOT,.mono))))) +$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibXamarinTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_NATIVEAOT_DEBUG,-dotnet-nativeaot-debug,_NATIVEAOT,.mono))))) # # DotNetLibExtensionTemplate builds lib[tv]extension.a @@ -731,6 +749,7 @@ endef $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibExtensionTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),,-dotnet,,))))) $(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibExtensionTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_CORECLR,-dotnet-coreclr,_CORECLR))))) +$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(foreach arch,$(DOTNET_$(rid)_ARCHITECTURES),$(eval $(call DotNetLibExtensionTemplate,$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),$(arch),_NATOVEAOT,-dotnet-nativeaot,_NATOVEAOT))))) dotnet: $(DOTNET_TARGETS) diff --git a/runtime/coreclr-bridge.m b/runtime/coreclr-bridge.m index 287c3f963534..80242c601c15 100644 --- a/runtime/coreclr-bridge.m +++ b/runtime/coreclr-bridge.m @@ -11,6 +11,7 @@ #include #include #include +#include #include "product.h" #include "runtime-internal.h" @@ -537,18 +538,20 @@ xamarin_assertion_message ("%s threw an exception: %p = %s", method, gchandle, [xamarin_print_all_exceptions (gchandle) UTF8String]); } -typedef void (*xamarin_runtime_initialize_decl)(struct InitializationOptions* options); +#if !defined (NATIVEAOT) +typedef void (*xamarin_runtime_initialize_decl)(struct InitializationOptions* options, GCHandle* exception_gchandle); void xamarin_bridge_call_runtime_initialize (struct InitializationOptions* options, GCHandle* exception_gchandle) { void *del = NULL; - int rv = coreclr_create_delegate (coreclr_handle, coreclr_domainId, PRODUCT ", Version=0.0.0.0", "ObjCRuntime.Runtime", "Initialize", &del); + int rv = coreclr_create_delegate (coreclr_handle, coreclr_domainId, PRODUCT ", Version=0.0.0.0", "ObjCRuntime.Runtime", "SafeInitialize", &del); if (rv != 0) xamarin_assertion_message ("xamarin_bridge_call_runtime_initialize: failed to create delegate: %i\n", rv); xamarin_runtime_initialize_decl runtime_initialize = (xamarin_runtime_initialize_decl) del; - runtime_initialize (options); + runtime_initialize (options, exception_gchandle); } +#endif // !defined (NATIVEAOT) void xamarin_bridge_register_product_assembly (GCHandle* exception_gchandle) diff --git a/runtime/nativeaot-bridge.m b/runtime/nativeaot-bridge.m new file mode 100644 index 000000000000..fc9da179319e --- /dev/null +++ b/runtime/nativeaot-bridge.m @@ -0,0 +1,35 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* +* Authors: Rolf Bjarne Kvinge +* +* Copyright (C) 2023 Microsoft Corp. +* +*/ + +#if defined (NATIVEAOT) + +#include +#include +#include +#include +#include + +#include "product.h" +#include "runtime-internal.h" +#include "slinked-list.h" +#include "xamarin/xamarin.h" +#include "xamarin/coreclr-bridge.h" + +typedef void (*xamarin_runtime_initialize_decl)(struct InitializationOptions* options, GCHandle* exception_gchandle); +void +xamarin_bridge_call_runtime_initialize (struct InitializationOptions* options, GCHandle* exception_gchandle) +{ + void *del = dlsym (RTLD_DEFAULT, "xamarin_objcruntime_runtime_nativeaotinitialize"); + if (del == NULL) + xamarin_assertion_message ("xamarin_bridge_call_runtime_initialize: failed to load xamarin_objcruntime_runtime_nativeaotinitialize: %s\n", dlerror ()); + + xamarin_runtime_initialize_decl runtime_initialize = (xamarin_runtime_initialize_decl) del; + runtime_initialize (options, exception_gchandle); +} + +#endif // NATIVEAOT diff --git a/runtime/runtime.m b/runtime/runtime.m index 323007930733..9733c0c279e6 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -1304,12 +1304,14 @@ -(void) xamarinSetFlags: (enum XamarinGCHandleFlags) flags; #endif #if defined (CORECLR_RUNTIME) +#if !defined(__arm__) // the dynamic trampolines haven't been implemented in 32-bit ARM assembly. options.xamarin_objc_msgsend = (void *) xamarin_dyn_objc_msgSend; options.xamarin_objc_msgsend_super = (void *) xamarin_dyn_objc_msgSendSuper; #if !defined(__aarch64__) options.xamarin_objc_msgsend_stret = (void *) xamarin_dyn_objc_msgSend_stret; options.xamarin_objc_msgsend_super_stret = (void *) xamarin_dyn_objc_msgSendSuper_stret; #endif // !defined(__aarch64__) +#endif // !defined(__arm__) options.unhandled_exception_handler = (void *) &xamarin_coreclr_unhandled_exception_handler; options.reference_tracking_begin_end_callback = (void *) &xamarin_coreclr_reference_tracking_begin_end_callback; options.reference_tracking_is_referenced_callback = (void *) &xamarin_coreclr_reference_tracking_is_referenced_callback; diff --git a/src/ObjCRuntime/Runtime.cs b/src/ObjCRuntime/Runtime.cs index f43c10c03928..0333bcf8cb08 100644 --- a/src/ObjCRuntime/Runtime.cs +++ b/src/ObjCRuntime/Runtime.cs @@ -237,6 +237,20 @@ internal static bool Initialized { static extern int _NSGetExecutablePath (byte[] buf, ref int bufsize); #endif +#if NET + [Preserve] // called from native - nativeaot-bridge.m and coreclr-bridge.m. + [UnmanagedCallersOnly (EntryPoint = "xamarin_objcruntime_runtime_nativeaotinitialize")] + unsafe static void SafeInitialize (InitializationOptions* options, IntPtr* exception_gchandle) + { + *exception_gchandle = IntPtr.Zero; + try { + Initialize (options); + } catch (Exception e) { + *exception_gchandle = AllocGCHandle (e); + } + } +#endif + [Preserve] // called from native - runtime.m. [BindingImpl (BindingImplOptions.Optimizable)] // To inline the Runtime.DynamicRegistrationSupported code if possible. unsafe static void Initialize (InitializationOptions* options)