Bläddra i källkod

Initial Mono.Native Support. (#10596)

`Mono.Native` is the new name for both `System.Native` and `System.Security.Cryptography.Apple`.
It is built as a stand-alone shared library and not bundled with the runtime because we
may need to build two different versions of it.

Starting with macOS 10.12+ and iOS 10+, Apple introduced a new Unified API for some of the
crypto primitives that we're using as part of System.Security.Cryptography.Apple.

On Desktop, we can check at runtime whether the OS version is recent enough and switch
implementation accordingly.  We build a single `libmono_native` shared library.

However, on Mobile we cannot have any undefined symbols as this would break Bitcode.

During the mobile build, we are called with `CC` containing an explicit minium version flag,
which is either `-mmacosx-version-min=`, `-mios-simulator-version-min=` or `-miphoneos-version-min=`
depending on platform.

We build two versions of the shared library:
- `libmono_native_compat` is built with whichever minimum version is passed to us via `CC`.
- `libmono_native_unifed` is built with the minimum version set to macOS 10.12+ / iOS 10+.

For testing purposes, there is a function called `mono_native_get_platform_type ()`
(see mono/native/mono-native-platform.c), which returns a `MonoNativePlatformType` enum value.
There is also `Mono.MonoNativePlatform.GetPlatformType ()` (see mcs/class/corlib/Mono/MonoNativePlatform.cs).

This can be called by automated tests both to ensure that the library has been correctly installed and also
to verify that it's the correct version of it.

* `configure.ac`: add new configure checks for Mono.Native.

* `mono/native/`: new directory - this replaces the `System.Native` code that was previously in `mono/metadata`.

* `mcs/class/corlib/Mono`: New internal `MonoNativePlatformType` and `MonoNativePlatform` classes.
Martin Baulig 7 år sedan
förälder
incheckning
51548b4d37

+ 217 - 1
configure.ac

@@ -2967,7 +2967,7 @@ if test x$host_win32 = xno; then
 	fi
 
 	dnl *********************************
-	dnl *** Checks for System.Native  ***
+	dnl *** Checks for Mono.Native  ***
 	dnl *********************************
 
 	AC_MSG_CHECKING(for linux/in.h)
@@ -4308,6 +4308,7 @@ if test "x$target_mach" = "xyes"; then
 	  CPPFLAGS_FOR_LIBGC="$CPPFLAGS_FOR_LIBGC -DTARGET_IOS"
 	  CFLAGS_FOR_LIBGC="$CFLAGS_FOR_LIBGC -DTARGET_IOS"
 	  BTLS_SUPPORTED=no
+	  target_ios=yes
    else
        AC_TRY_COMPILE([#include "TargetConditionals.h"],[
        #if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
@@ -5515,6 +5516,201 @@ if test x"$GCC" = xyes; then
 	])
 fi
 
+#
+# Mono.Native Support
+# -------------------
+# Mono.Native is the new name for both System.Native and System.Security.Cryptography.Apple.
+# It is built as a stand-alone shared library and not bundled with the runtime because we
+# may need to build two different versions of it.
+#
+# Starting with macOS 10.12+ and iOS 10+, Apple introduced a new Unified API for some of the
+# crypto primitives that we're using as part of System.Security.Cryptography.Apple.
+#
+# On Desktop, we can check at runtime whether the OS version is recent enough and switch
+# implementation accordingly.  We build a single `libmono_native` shared library.
+#
+# However, on Mobile we cannot have any undefined symbols as this would break Bitcode.
+#
+# During the mobile build, we are called with `CC` containing an explicit minium version flag,
+# which is eiter `-mmacosx-version-min=`, `-mios-simulator-version-min=` or `-miphoneos-version-min=`
+# depending on platform.
+#
+# We build two versions of the shared library:
+# - `libmono_native_compat` is built with whichever minimum version is passed to us via `CC`.
+# - `libmono_native_unifed` is built with the minimum version set to macOS 10.12+ / iOS 10+.
+#
+# For testing purpuses, there is a function called `mono_native_get_platform_type ()`
+# (see mono/native/mono-native-platform.c), which returns a `MonoNativePlatformType` enum value.
+# There is also `Mono.MonoNativePlatform.GetPlatformType ()` (see mcs/class/System/Mono/MonoNativePlatform.cs).
+#
+# This can be called by automated tests both to ensure that the library has been correctly installed and also
+# to verify that it's the correct version of it.
+#
+
+# `target_ios=yes` does not detect watch devices and fails when cross-compiling
+AC_MONO_APPLE_TARGET(TARGET_OS_IPHONE, [mono_native_platform_ios=yes])
+
+sed_version_pattern='[[0-9]]\{1,2\}\(\.[[0-9]]\{1,2\}\)'
+if test x$target_osx = xyes; then
+	sed_remove_mac_version_pattern="s/-mmacosx-version-min=$sed_version_pattern//g"
+	grep_find_mac_version_pattern="-mmacosx-version-min=$sed_version_pattern"
+	mono_native_compat_version=`echo $CC | grep -o -e $grep_find_mac_version_pattern`
+	MONO_NATIVE_CC=`echo $CC | sed -e $sed_remove_mac_version_pattern`
+	MONO_NATIVE_CXX=`echo $CXX | sed -e $sed_remove_mac_version_pattern`
+	MONO_NATIVE_CPPFLAGS=`echo $CPPFLAGS | sed -e $sed_remove_mac_version_pattern`
+	MONO_NATIVE_CFLAGS=`echo $CFLAGS | sed -e $sed_remove_mac_version_pattern`
+	MONO_NATIVE_CXXFLAGS=`echo $CXXFLAGS | sed -e $sed_remove_mac_version_pattern`
+	MONO_NATIVE_LDFLAGS=`echo $LDFLAGS | sed -e $sed_remove_mac_version_pattern`
+
+	mono_native=yes
+	MONO_NATIVE_PLATFORM=macos
+	MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_MACOS"
+	AC_MSG_CHECKING([Mono.Native support])
+	AC_MSG_RESULT(macos)
+
+	AC_MONO_APPLE_AVAILABLE(mono_native_compat, [whether we need the compatibility layer],
+		[!(MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12)])
+
+	if test x$mono_native_compat = xyes; then
+		mono_native_compat=yes
+		mono_native_text="Mac OSX (compat+unified)"
+		MONO_NATIVE_COMPAT_CFLAGS="$CFLAGS $mono_native_compat_version"
+		MONO_NATIVE_COMPAT_LDFLAGS="$LDFLAGS $mono_native_compat_version"
+		MONO_NATIVE_UNIFIED_CFLAGS="$MONO_NATIVE_CFLAGS -mmacosx-version-min=10.12"
+		MONO_NATIVE_UNIFIED_LDFLAGS="$MONO_NATIVE_LDFLAGS -mmacosx-version-min=10.12"
+	else
+		mono_native_compat=no
+		mono_native_text="Mac OSX"
+	fi
+elif test x$mono_native_platform_ios = xyes; then
+	AC_MONO_APPLE_TARGET(TARGET_OS_SIMULATOR, [mono_native_ios_sim=yes], [mono_native_ios_sim=no])
+
+	AC_MONO_APPLE_TARGET(TARGET_OS_IOS, [
+		if test x$mono_native_ios_sim = xyes; then
+			mono_native_ios_target=ios-simulator
+			mono_native_text="iOS Simulator"
+			MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_IPHONE | MONO_NATIVE_PLATFORM_TYPE_SIMULATOR"
+		else
+			mono_native_ios_target=iphoneos
+			mono_native_text="iOS Device"
+			MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_IPHONE | MONO_NATIVE_PLATFORM_TYPE_DEVICE"
+		fi
+		mono_native_unified_version="10.0"
+		mono_native_compat_check="__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0"
+	], [
+		AC_MONO_APPLE_TARGET(TARGET_OS_TV, [
+			if test x$mono_native_ios_sim = xyes; then
+				mono_native_ios_target=tvos-simulator
+				mono_native_text="AppleTV Simulator"
+				MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_TV | MONO_NATIVE_PLATFORM_TYPE_SIMULATOR"
+			else
+				mono_native_ios_target=tvos
+				mono_native_text="AppleTV Device"
+				MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_TV | MONO_NATIVE_PLATFORM_TYPE_DEVICE"
+			fi
+			mono_native_unified_version="10.0"
+			mono_native_compat_check="__TV_OS_VERSION_MIN_REQUIRED >= __TVOS_10_0"
+		], [
+			AC_MONO_APPLE_TARGET(TARGET_OS_WATCH, [
+				if test x$mono_native_ios_sim = xyes; then
+					mono_native_ios_target=watchos-simulator
+					mono_native_text="Apple Watch Simulator"
+					MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_WATCH | MONO_NATIVE_PLATFORM_TYPE_SIMULATOR"
+				else
+					mono_native_ios_target=watchos
+					mono_native_text="Apple Watch Device"
+					MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_IOS | MONO_NATIVE_PLATFORM_TYPE_WATCH | MONO_NATIVE_PLATFORM_TYPE_DEVICE"
+				fi
+				mono_native_unified_version="5.0"
+				mono_native_compat_check="__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_5_0"
+			], [
+				AC_MSG_ERROR([Unknown iOS Target])
+			])
+		])
+	])
+
+	AC_MSG_CHECKING([Mono.Native support])
+	AC_MSG_RESULT([$mono_native_ios_target])
+
+	AC_MONO_APPLE_AVAILABLE(mono_native_compat, [whether we need the compatibility layer], [!($mono_native_compat_check)])
+
+	sed_remove_ios_version_pattern="s/-m\(.*\)-version-min=$sed_version_pattern//g"
+	grep_find_ios_version_pattern="-m$mono_native_ios_target-version-min=$sed_version_pattern"
+
+	mono_native_compat_version=`echo $CC | grep -o -e $grep_find_ios_version_pattern`
+
+	mono_native_ldflags="-framework CoreFoundation -framework Foundation -no-undefined -fatal_warnings"
+	MONO_NATIVE_CC=`echo $CC | sed -e $sed_remove_ios_version_pattern`
+	MONO_NATIVE_CXX=`echo $CXX | sed -e $sed_remove_ios_version_pattern`
+	MONO_NATIVE_CPPFLAGS=`echo $CPPFLAGS | sed -e $sed_remove_ios_version_pattern`
+	MONO_NATIVE_CXXFLAGS=`echo $CXXFLAGS | sed -e $sed_remove_ios_version_pattern`
+	MONO_NATIVE_CFLAGS=`echo $CFLAGS | sed -e $sed_remove_ios_version_pattern`
+	MONO_NATIVE_LDFLAGS=`echo $LDFLAGS $mono_native_ldflags | sed -e $sed_remove_ios_version_pattern`
+
+	if test x$mono_native_compat = xyes; then
+		mono_native_text="$mono_native_text (compat+unified)"
+		MONO_NATIVE_COMPAT_CFLAGS="$CFLAGS $mono_native_compat_version"
+		MONO_NATIVE_COMPAT_LDFLAGS="$LDFLAGS $mono_native_compat_version"
+		MONO_NATIVE_UNIFIED_CFLAGS="$MONO_NATIVE_CFLAGS -m$mono_native_ios_target-version-min=$mono_native_unified_version"
+		MONO_NATIVE_UNIFIED_LDFLAGS="$MONO_NATIVE_LDFLAGS -m$mono_native_ios_target-version-min=$mono_native_unified_version"
+	fi
+
+	mono_native=yes
+	MONO_NATIVE_PLATFORM=ios
+elif test x$host_linux = xyes; then
+	mono_native_text="Linux"
+	MONO_NATIVE_CC=$CC
+	MONO_NATIVE_CXX=$CXX
+	MONO_NATIVE_CPPFLAGS=$CPPFLAGS
+	MONO_NATIVE_CXXFLAGS=$CXXFLAGS
+	MONO_NATIVE_CFLAGS=$CFLAGS
+	MONO_NATIVE_LDFLAGS=$LDFLAGS
+
+	mono_native=yes
+	mono_native_compat=no
+	MONO_NATIVE_PLATFORM=linux
+	AC_MSG_CHECKING([Mono.Native support])
+	AC_MSG_RESULT(linux)
+
+	MONO_NATIVE_PLATFORM_TYPE="MONO_NATIVE_PLATFORM_TYPE_LINUX"
+else
+	mono_native=no
+	mono_native_text="no"
+	AC_MSG_CHECKING([Mono.Native support])
+	AC_MSG_RESULT(no)
+fi
+
+if test x$mono_native_compat = xyes; then
+	MONO_NATIVE_LIBRARY_NAME=libmono-native-compat
+else
+	MONO_NATIVE_LIBRARY_NAME=libmono-native
+fi
+
+AC_SUBST(MONO_NATIVE_PLATFORM)
+AC_SUBST(MONO_NATIVE_CC)
+AC_SUBST(MONO_NATIVE_CXX)
+AC_SUBST(MONO_NATIVE_CPPFLAGS)
+AC_SUBST(MONO_NATIVE_CXXFLAGS)
+AC_SUBST(MONO_NATIVE_CFLAGS)
+AC_SUBST(MONO_NATIVE_COMPAT_CFLAGS)
+AC_SUBST(MONO_NATIVE_UNIFIED_CFLAGS)
+AC_SUBST(MONO_NATIVE_LDFLAGS)
+AC_SUBST(MONO_NATIVE_COMPAT_LDFLAGS)
+AC_SUBST(MONO_NATIVE_UNIFIED_LDFLAGS)
+AC_SUBST(MONO_NATIVE_LIBRARY_NAME)
+
+AM_CONDITIONAL(MONO_NATIVE, test x$mono_native = xyes)
+AM_CONDITIONAL(MONO_NATIVE_COMPAT, test x$mono_native_compat = xyes)
+AM_CONDITIONAL(MONO_NATIVE_PLATFORM_MACOS, test x$MONO_NATIVE_PLATFORM = xmacos)
+AM_CONDITIONAL(MONO_NATIVE_PLATFORM_IOS, test x$MONO_NATIVE_PLATFORM = xios)
+AM_CONDITIONAL(MONO_NATIVE_PLATFORM_LINUX, test x$MONO_NATIVE_PLATFORM = xlinux)
+
+MONO_NATIVE_PLATFORM_TYPE_COMPAT="$MONO_NATIVE_PLATFORM_TYPE | MONO_NATIVE_PLATFORM_TYPE_COMPAT"
+MONO_NATIVE_PLATFORM_TYPE_UNIFIED="$MONO_NATIVE_PLATFORM_TYPE | MONO_NATIVE_PLATFORM_TYPE_UNIFIED"
+AC_SUBST(MONO_NATIVE_PLATFORM_TYPE)
+AC_SUBST(MONO_NATIVE_PLATFORM_TYPE_COMPAT)
+AC_SUBST(MONO_NATIVE_PLATFORM_TYPE_UNIFIED)
+
 # Update all submodules recursively to ensure everything is checked out
 (cd $srcdir && scripts/update_submodules.sh)
 
@@ -5528,6 +5724,7 @@ scripts/mono-find-requires
 mk/Makefile
 mono/Makefile
 mono/btls/Makefile
+mono/native/Makefile
 mono/utils/Makefile
 mono/utils/jemalloc/Makefile
 mono/metadata/Makefile
@@ -5543,6 +5740,9 @@ mono/arch/arm/Makefile
 mono/arch/arm64/Makefile
 mono/arch/mips/Makefile
 mono/sgen/Makefile
+mono/native/platform-type.c
+mono/native/platform-type-compat.c
+mono/native/platform-type-unified.c
 mono/tests/Makefile
 mono/tests/assembly-load-reference/Makefile
 mono/tests/tests-config
@@ -5724,6 +5924,21 @@ fi
       echo "HAVE_BTLS=1" >> $srcdir/$mcsdir/build/config.make
     fi
 
+    if test "x$mono_native" = "xyes"; then
+      echo "MONO_NATIVE_SUPPORTED=true" >> $srcdir/$mcsdir/build/config.make
+    else
+      echo "MONO_NATIVE_SUPPORTED=false" >> $srcdir/$mcsdir/build/config.make
+    fi
+
+    if test x$mono_native_compat = xyes; then
+      echo "MONO_NATIVE_USING_COMPAT=true" >> $srcdir/$mcsdir/build/config.make
+    else
+      echo "MONO_NATIVE_USING_COMPAT=false" >> $srcdir/$mcsdir/build/config.make
+    fi
+
+    echo "MONO_NATIVE_PLATFORM_TYPE=$MONO_NATIVE_PLATFORM_TYPE" >> $srcdir/$mcsdir/build/config.make
+    echo "MONO_NATIVE_PLATFORM=$MONO_NATIVE_PLATFORM" >> $srcdir/$mcsdir/build/config.make
+
   fi
 
 )
@@ -5766,6 +5981,7 @@ echo "
 	DTrace:        $enable_dtrace
 	LLVM Back End: $enable_llvm (dynamically loaded: $enable_loadedllvm, built in-tree: $internal_llvm, assertions: $enable_llvm_asserts)
 	Spectre:       $spectre_mitigation_status
+	Mono.Native:   $mono_native_text
 
    Libraries:
 	.NET 4.x:        $with_profile4_x

+ 3 - 2
data/config.in

@@ -11,8 +11,9 @@
 	<dllmap dll="oci" target="libclntsh@libsuffix@" os="!windows"/>
 	<dllmap dll="db2cli" target="libdb2_36@libsuffix@" os="!windows"/>
 	<dllmap dll="MonoPosixHelper" target="$mono_libdir/libMonoPosixHelper@libsuffix@" os="!windows" />
-	<dllmap dll="System.Native" target="$mono_libdir/libmono-system-native@libsuffix@" os="!windows" />
-	<dllmap dll="System.Net.Security.Native" target="$mono_libdir/libmono-system-net-security-native@libsuffix@" os="!windows" />
+	<dllmap dll="System.Native" target="$mono_libdir/@MONO_NATIVE_LIBRARY_NAME@@libsuffix@" os="!windows" />
+	<dllmap dll="System.Net.Security.Native" target="$mono_libdir/@MONO_NATIVE_LIBRARY_NAME@@libsuffix@" os="!windows" />
+	<dllmap dll="System.Security.Cryptography.Native.Apple" target="$mono_libdir/@MONO_NATIVE_LIBRARY_NAME@@libsuffix@" os="osx" />
 	<dllmap dll="libmono-btls-shared" target="$mono_libdir/libmono-btls-shared@libsuffix@" os="!windows" />
 	<dllmap dll="i:msvcrt" target="@LIBC@" os="!windows"/>
 	<dllmap dll="i:msvcrt.dll" target="@LIBC@" os="!windows"/>

+ 40 - 0
m4/apple-target.m4

@@ -0,0 +1,40 @@
+# Usage:
+# AC_MONO_APPLE_TARGET(target-name, action-if-found, action-if-not-found)
+#
+# Checks whether `target-name` is defined in "TargetConditionals.h"
+#
+AC_DEFUN([AC_MONO_APPLE_TARGET], [
+	AC_MONO_APPLE_AVAILABLE([$1], [for $1], [$1 == 1], $2, $3)
+])
+
+
+# Usage:
+# AC_MONO_APPLE_AVAILABLE(name, message, conditional, action-if-found, action-if-not-found)
+#
+# Checks for `conditional` using "TargetConditionals.h" and "AvailabilityMacros.h"
+#
+AC_DEFUN([AC_MONO_APPLE_AVAILABLE], [
+	# cache the compilation check
+	AC_CACHE_CHECK([$2], [ac_cv_apple_available_[]$1], [
+		AC_TRY_COMPILE([
+			#include "TargetConditionals.h"
+			#include "AvailabilityMacros.h"
+		],[
+			#if !($3)
+			#error failed
+			#endif
+		], [
+			ac_cv_apple_available_[]$1=yes
+		], [
+			ac_cv_apple_available_[]$1=no
+		])
+	])
+	# keep the actions out of the cache check because they need to be always executed.
+	if test x$ac_cv_apple_available_[]$1 = xyes; then
+		$1=yes
+		[$4]
+	else
+		$1=no
+		[$5]
+	fi
+])

+ 13 - 1
mcs/build/Makefile

@@ -2,7 +2,7 @@ thisdir = build
 SUBDIRS = 
 include ../build/rules.make
 
-BUILT_FILES = common/Consts.cs
+BUILT_FILES = common/Consts.cs common/MonoNativeConfig.cs
 
 all-local install-local test-local run-test-local csproj-local run-test-ondotnet-local uninstall-local doc-update-local: $(BUILT_FILES)
 	@:
@@ -15,6 +15,18 @@ common/Consts.cs: common/Consts.cs.in $(wildcard config.make)
 	test -n '$(MONO_CORLIB_VERSION)'
 	sed -e 's,@''MONO_VERSION@,$(MONO_VERSION),' -e 's,@''MONO_CORLIB_VERSION@,$(MONO_CORLIB_VERSION),' $< > $@
 
+#
+# This is only used by corlib/Test/Mono/NativePlatformTest.cs, to ensure that we're using the correct native library.
+# The test is disabled on all Windows platforms.
+#
+common/MonoNativeConfig.cs: common/MonoNativeConfig.cs.in $(wildcard config.make)
+	test -n '$(MONO_NATIVE_SUPPORTED)'
+	sed -e 's,@''MONO_NATIVE_SUPPORTED@,$(MONO_NATIVE_SUPPORTED),' \
+		-e 's,@''MONO_NATIVE_USING_COMPAT@,$(MONO_NATIVE_USING_COMPAT),' \
+		-e 's,@''MONO_NATIVE_PLATFORM_TYPE@,$(MONO_NATIVE_PLATFORM_TYPE),' \
+		-e 's,@''MONO_NATIVE_PLATFORM@,$(MONO_NATIVE_PLATFORM),' \
+		$< > $@
+
 PLATFORMS = macos linux win32 unix
 PROFILES = \
 	basic \

+ 1 - 0
mcs/build/common/.gitignore

@@ -1 +1,2 @@
 /Consts.cs
+/MonoNativeConfig.cs

+ 10 - 0
mcs/build/common/MonoNativeConfig.cs.in

@@ -0,0 +1,10 @@
+namespace Mono
+{
+	static class MonoNativeConfig
+	{
+		internal const bool IsSupported = @MONO_NATIVE_SUPPORTED@;
+		internal const bool UsingCompat = @MONO_NATIVE_USING_COMPAT@;
+		internal const string PlatformTypeString = "@MONO_NATIVE_PLATFORM_TYPE@";
+		internal const string PlatformName = "@MONO_NATIVE_PLATFORM@";
+	}
+}

+ 40 - 0
mcs/class/corlib/Mono/MonoNativePlatform.cs

@@ -0,0 +1,40 @@
+//
+// MonoNativePlatform.cs
+//
+// Author:
+//       Martin Baulig <[email protected]>
+//
+// Copyright (c) 2018 Xamarin, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System.Runtime.InteropServices;
+
+namespace Mono
+{
+	static class MonoNativePlatform
+	{
+		[DllImport ("System.Native")]
+		extern static int mono_native_get_platform_type ();
+
+		public static MonoNativePlatformType GetPlatformType ()
+		{
+			return (MonoNativePlatformType)mono_native_get_platform_type ();
+		}
+	}
+}

+ 49 - 0
mcs/class/corlib/Mono/MonoNativePlatformType.cs

@@ -0,0 +1,49 @@
+//
+// MonoNativePlatformType.cs
+//
+// Author:
+//       Martin Baulig <[email protected]>
+//
+// Copyright (c) 2018 Xamarin, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+
+namespace Mono
+{
+	// Keep in sync with mono/native/mono-native-platform-type.h
+	[Flags]
+	enum MonoNativePlatformType
+	{
+		MONO_NATIVE_PLATFORM_TYPE_UNKNOWN	= 0,
+		MONO_NATIVE_PLATFORM_TYPE_MACOS		= 1,
+		MONO_NATIVE_PLATFORM_TYPE_IOS		= 2,
+		MONO_NATIVE_PLATFORM_TYPE_LINUX		= 3,
+
+		MONO_NATIVE_PLATFORM_TYPE_IPHONE	= 0x100,
+		MONO_NATIVE_PLATFORM_TYPE_TV		= 0x200,
+		MONO_NATIVE_PLATFORM_TYPE_WATCH		= 0x400,
+
+		MONO_NATIVE_PLATFORM_TYPE_COMPAT	= 0x1000,
+		MONO_NATIVE_PLATFORM_TYPE_UNIFIED	= 0x2000,
+
+		MONO_NATIVE_PLATFORM_TYPE_SIMULATOR	= 0x4000,
+		MONO_NATIVE_PLATFORM_TYPE_DEVICE	= 0x8000
+	}
+}

+ 28 - 0
mcs/class/corlib/Test/Mono/NativePlatformTest.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Runtime.InteropServices;
+using NUnit.Framework;
+using Mono;
+
+namespace MonoTests.Mono
+{
+	[TestFixture]
+	public class NativePlatformTest
+	{
+		[TestFixtureSetUp]
+		public void SetUp ()
+		{
+			if (!MonoNativeConfig.IsSupported)
+				Assert.Ignore ("Mono.Native is not supported on this platform.");
+		}
+
+		[Test]
+		public void Test ()
+		{
+			var type = MonoNativePlatform.GetPlatformType ();
+			Assert.That ((int)type, Is.GreaterThan (0), "platform type");
+
+			var usingCompat = (type & MonoNativePlatformType.MONO_NATIVE_PLATFORM_TYPE_COMPAT) != 0;
+			Assert.AreEqual (MonoNativeConfig.UsingCompat, usingCompat, "using compatibility layer");
+		}
+	}
+}

+ 14 - 0
mcs/class/corlib/corlib.csproj

@@ -2041,6 +2041,8 @@
         <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+        <Compile Include="Mono\MonoNativePlatform.cs" />
+        <Compile Include="Mono\MonoNativePlatformType.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2315,6 +2317,8 @@
             <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+            <Compile Include="Mono\MonoNativePlatform.cs" />
+            <Compile Include="Mono\MonoNativePlatformType.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2351,6 +2355,8 @@
             <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+            <Compile Include="Mono\MonoNativePlatform.cs" />
+            <Compile Include="Mono\MonoNativePlatformType.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2387,6 +2393,8 @@
             <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
             <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+            <Compile Include="Mono\MonoNativePlatform.cs" />
+            <Compile Include="Mono\MonoNativePlatformType.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
             <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2420,6 +2428,8 @@
         <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+        <Compile Include="Mono\MonoNativePlatform.cs" />
+        <Compile Include="Mono\MonoNativePlatformType.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2452,6 +2462,8 @@
         <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+        <Compile Include="Mono\MonoNativePlatform.cs" />
+        <Compile Include="Mono\MonoNativePlatformType.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />
@@ -2484,6 +2496,8 @@
         <Compile Include="Microsoft.Win32\UnixRegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32RegistryApi.cs" />
         <Compile Include="Microsoft.Win32\Win32ResultCode.cs" />
+        <Compile Include="Mono\MonoNativePlatform.cs" />
+        <Compile Include="Mono\MonoNativePlatformType.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAccessRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistryAuditRule.cs" />
         <Compile Include="System.Security.AccessControl\RegistrySecurity.cs" />

+ 4 - 0
mcs/class/corlib/corlib_test.dll.sources

@@ -1,6 +1,10 @@
 Microsoft.Win32/RegistryKeyTest.cs
+../../../build/common/MonoNativeConfig.cs
 Mono/DataConvertTest.cs
 ../Mono/DataConverter.cs
+Mono/NativePlatformTest.cs
+../Mono/MonoNativePlatform.cs
+../Mono/MonoNativePlatformType.cs
 System/ActivatorTest.cs
 System/AppDomainManagerTest.cs
 System/AppDomainSetupTest.cs

+ 2 - 0
mcs/class/corlib/monotouch_corlib.dll.sources

@@ -7,6 +7,8 @@ CommonCrypto/RijndaelManaged.cs
 CommonCrypto/RNGCryptoServiceProvider.cryptor.cs
 CommonCrypto/RC4CommonCrypto.cs
 CoreFoundation/CFHelpers.cs
+Mono/MonoNativePlatform.cs
+Mono/MonoNativePlatformType.cs
 System.Text/EncodingHelper.MonoTouch.cs
 System/Environment.iOS.cs
 System/Guid.MonoTouch.cs

+ 4 - 0
mcs/class/corlib/testing_aot_full_corlib_test.dll.exclude.sources

@@ -1,3 +1,7 @@
+../../../build/common/MonoNativeConfig.cs
+Mono/NativePlatformTest.cs
+../Mono/MonoNativePlatform.cs
+../Mono/MonoNativePlatformType.cs
 Microsoft.Win32/RegistryKeyTest.cs
 System.Diagnostics.Contracts/ContractAssertTest.cs
 System.Diagnostics.Contracts/ContractAssumeTest.cs

+ 3 - 0
mcs/class/corlib/unix_net_4_x_corlib.dll.sources

@@ -1,4 +1,7 @@
 #include net_4_x_corlib.dll.sources
 
+Mono/MonoNativePlatform.cs
+Mono/MonoNativePlatformType.cs
+
 ../../../external/corefx/src/Common/src/CoreLib/Interop/Unix/System.Native/Interop.GetRandomBytes.cs
 ../../../external/corefx/src/Common/src/CoreLib/Interop/Unix/Interop.Libraries.cs

+ 1 - 0
mcs/class/corlib/win32_net_4_x_corlib_test.dll.exclude.sources

@@ -0,0 +1 @@
+#include testing_aot_full_corlib_test.dll.exclude.sources

+ 8 - 4
mono/Makefile.am

@@ -8,11 +8,15 @@ if BTLS
 btls_dirs = btls
 endif
 
+if MONO_NATIVE
+native_dirs = native
+endif
+
 if CROSS_COMPILING
-SUBDIRS = $(btls_dirs) eglib arch utils cil $(sgen_dirs) metadata mini dis profiler
+SUBDIRS = $(btls_dirs) $(native_dirs) eglib arch utils cil $(sgen_dirs) metadata mini dis profiler
 else
 if INSTALL_MONOTOUCH
-SUBDIRS = $(btls_dirs) eglib arch utils $(sgen_dirs) metadata mini profiler
+SUBDIRS = $(btls_dirs) $(native_dirs) eglib arch utils $(sgen_dirs) metadata mini profiler
 
 monotouch-do-build:
 	@list='$(SUBDIRS)'; for subdir in $$list; do \
@@ -36,7 +40,7 @@ monotouch-do-clean:
 	  (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$target); \
     done;
 else
-SUBDIRS = $(btls_dirs) eglib arch utils cil $(sgen_dirs) metadata mini dis tests unit-tests benchmark profiler
+SUBDIRS = $(btls_dirs) $(native_dirs) eglib arch utils cil $(sgen_dirs) metadata mini dis tests unit-tests benchmark profiler
 endif
 endif
-DIST_SUBDIRS = btls eglib arch utils cil $(sgen_dirs) metadata mini dis tests unit-tests benchmark profiler
+DIST_SUBDIRS = btls native eglib arch utils cil $(sgen_dirs) metadata mini dis tests unit-tests benchmark profiler

+ 0 - 73
mono/metadata/Makefile.am

@@ -148,79 +148,6 @@ libmono_ilgen_la_LIBADD = $(glib_libs) ../utils/libmonoutils.la ../sgen/libmonos
 endif
 endif
 
-system_native_common_sources = \
-	pal_config.h \
-	pal-icalls.h \
-	pal-icalls.c \
-	../../external/corefx/src/Native/Unix/Common/pal_compiler.h \
-	../../external/corefx/src/Native/Unix/Common/pal_safecrt.h \
-	../../external/corefx/src/Native/Unix/Common/pal_types.h \
-	../../external/corefx/src/Native/Unix/Common/pal_utilities.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_errno.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_errno.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_maphardwaretype.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_maphardwaretype.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_memory.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_memory.h
-
-libmono_system_net_security_native_la_SOURCES = system_net_security_native_empty.c
-
-if ENABLE_GSS
-libmono_system_net_security_native_la_SOURCES += \
-	pal_config.h \
-	../../external/corefx/src/Native/Unix/Common/pal_compiler.h \
-	../../external/corefx/src/Native/Unix/Common/pal_types.h \
-	../../external/corefx/src/Native/Unix/Common/pal_utilities.h \
-	../../external/corefx/src/Native/Unix/System.Net.Security.Native/pal_gssapi.c \
-	../../external/corefx/src/Native/Unix/System.Net.Security.Native/pal_gssapi.h
-endif
-
-system_native_full_sources = \
-	../../external/corefx/src/Native/Unix/System.Native/pal_io.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_io.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_networking.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_networking.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_networkstatistics.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_networkstatistics.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_tcpstate.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_tcpstate.h \
-	../../external/corefx/src/Native/Unix/System.Native/pal_random.c \
-	../../external/corefx/src/Native/Unix/System.Native/pal_random.h
-
-system_native_ios_sources = \
-	pal-ios.h \
-	pal-ios.c
-
-system_native_android_sources = \
-	pal-android.h \
-	pal-android.c
-
-if !HOST_WIN32
-if !HOST_WASM
-lib_LTLIBRARIES += libmono-system-native.la
-lib_LTLIBRARIES += libmono-system-net-security-native.la
-libmono_system_native_la_SOURCES = $(system_native_common_sources)
-if TARGET_OSX
-libmono_system_native_la_SOURCES += $(system_native_full_sources)
-else
-if ENABLE_MONODROID
-libmono_system_native_la_SOURCES += $(system_native_android_sources)
-else
-if ENABLE_MONOTOUCH
-libmono_system_native_la_SOURCES += $(system_native_ios_sources)
-else
-libmono_system_native_la_SOURCES += $(system_native_full_sources)
-endif
-endif
-endif
-endif
-endif
-
-# Add back CXX_REMOVE_CFLAGS to keep this as C until/unless https://github.com/dotnet/corefx/pull/31342.
-libmono_system_native_la_CFLAGS = -I$(abs_top_srcdir)/external/corefx/src/Native/Unix/Common -I$(abs_top_srcdir)/external/corefx/src/Native/Unix/System.Native -Wno-typedef-redefinition @CXX_REMOVE_CFLAGS@
-libmono_system_native_la_LDFLAGS = $(libmonoldflags)
-libmono_system_net_security_native_la_CFLAGS = $(libmono_system_native_la_CFLAGS)
-libmono_system_net_security_native_la_LDFLAGS = $(libmono_system_native_la_LDFLAGS)
 
 CLEANFILES = mono-bundle.stamp
 

+ 12 - 0
mono/native/.gitignore

@@ -0,0 +1,12 @@
+/Makefile
+/Makefile.in
+/Makefile.am
+/.libs
+/.deps
+/*.lo
+/*.la
+/*.o
+/*.a
+/platform-type.c
+/platform-type-compat.c
+/platform-type-unified.c

+ 112 - 0
mono/native/Makefile.am

@@ -0,0 +1,112 @@
+if MONO_NATIVE_COMPAT
+lib_LTLIBRARIES = libmono-native-compat.la libmono-native-unified.la
+else
+lib_LTLIBRARIES = libmono-native.la
+endif
+
+CC = $(MONO_NATIVE_CC)
+CCLD = $(MONO_NATIVE_CC)
+CCAS = $(MONO_NATIVE_CC)
+CPP = $(MONO_NATIVE_CC) -E
+CXX = $(MONO_NATIVE_CXX)
+CXXCPP = $(MONO_NATIVE_CXX) -E
+CC_FOR_BUILD = $(MONO_NATIVE_CC)
+CPPFLAGS = $(MONO_NATIVE_CPPFLAGS)
+CXXFLAGS = $(MONO_NATIVE_CXXFLAGS)
+LDFLAGS = $(MONO_NATIVE_LDFLAGS)
+
+AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/mono $(LIBGC_CPPFLAGS) $(GLIB_CFLAGS) $(SHARED_CFLAGS)
+
+common_sources = \
+	pal_config.h \
+	mono-native-platform.h \
+	mono-native-platform.c \
+	../../external/corefx/src/Native/Unix/Common/pal_compiler.h \
+	../../external/corefx/src/Native/Unix/Common/pal_safecrt.h \
+	../../external/corefx/src/Native/Unix/Common/pal_types.h \
+	../../external/corefx/src/Native/Unix/Common/pal_utilities.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_errno.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_errno.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_maphardwaretype.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_maphardwaretype.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_memory.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_memory.h
+
+macos_sources = $(unix_sources)
+
+ios_sources =
+
+linux_sources = $(unix_sources)
+
+unix_sources = \
+	pal-icalls.h \
+	pal-icalls.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_io.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_io.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_networking.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_networking.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_networkstatistics.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_networkstatistics.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_tcpstate.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_tcpstate.h \
+	../../external/corefx/src/Native/Unix/System.Native/pal_random.c \
+	../../external/corefx/src/Native/Unix/System.Native/pal_random.h
+
+gss_sources = \
+	../../external/corefx/src/Native/Unix/System.Net.Security.Native/pal_gssapi.c \
+	../../external/corefx/src/Native/Unix/System.Net.Security.Native/pal_gssapi.h
+
+if ENABLE_GSS
+macos_sources += $(gss_sources)
+endif
+
+EXTRA_libmono_native_la_SOURCES = $(common_sources) $(macos_sources) $(ios_sources) $(linux_sources) $(unix_sources) $(gss_sources)
+
+if MONO_NATIVE_PLATFORM_MACOS
+if MONO_NATIVE_COMPAT
+BCL_PROFILE = xammac
+else
+BCL_PROFILE = net_4_x
+endif
+platform_sources = $(macos_sources)
+else
+if MONO_NATIVE_PLATFORM_IOS
+BCL_PROFILE = monotouch
+platform_sources = $(ios_sources)
+else
+if MONO_NATIVE_PLATFORM_LINUX
+BCL_PROFILE = net_4_x
+platform_sources = $(linux_sources)
+endif
+endif
+endif
+
+common_cflags = \
+	-I$(abs_top_srcdir)/external/corefx/src/Native/Unix/Common \
+	-I$(abs_top_srcdir)/external/corefx/src/Native/Unix/System.Native \
+	-Wno-typedef-redefinition
+
+libmono_native_la_SOURCES = $(common_sources) $(platform_sources) platform-type.c
+
+libmono_native_la_CFLAGS = $(MONO_NATIVE_CFLAGS) $(common_cflags)
+
+libmono_native_la_LDFLAGS = $(MONO_NATIVE_LDFLAGS)
+
+libmono_native_compat_la_SOURCES = $(common_sources) $(platform_sources) platform-type-compat.c
+
+libmono_native_compat_la_CFLAGS = $(MONO_NATIVE_COMPAT_CFLAGS) $(common_cflags)
+
+libmono_native_compat_la_LDFLAGS = $(MONO_NATIVE_COMPAT_LDFLAGS)
+
+libmono_native_unified_la_SOURCES = $(common_sources) $(platform_sources) platform-type-unified.c
+
+libmono_native_unified_la_CFLAGS = $(MONO_NATIVE_UNIFIED_CFLAGS) $(common_cflags)
+
+libmono_native_unified_la_LDFLAGS = $(MONO_NATIVE_UNIFIED_LDFLAGS)
+
+MONO = ../../runtime/mono-wrapper
+BCL_BUILD_DIR = ../../mcs/class
+BCL_OUTPUT_DIR = $(BCL_BUILD_DIR)/lib/$(BCL_PROFILE)
+
+bcs_libs = mscorlib.dll System.dll
+

+ 10 - 0
mono/native/mono-native-platform.c

@@ -0,0 +1,10 @@
+#include "mono-native-platform.h"
+
+extern MonoNativePlatformType mono_native_platform_type;
+
+int32_t
+mono_native_get_platform_type (void)
+{
+	return mono_native_platform_type;
+}
+

+ 25 - 0
mono/native/mono-native-platform.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include "mono/utils/mono-publib.h"
+
+// Keep in sync with mcs/class/System/Mono/MonoNativePlatformType.cs
+
+typedef enum {
+	MONO_NATIVE_PLATFORM_TYPE_UNKNOWN	= 0,
+	MONO_NATIVE_PLATFORM_TYPE_MACOS		= 1,
+	MONO_NATIVE_PLATFORM_TYPE_IOS		= 2,
+	MONO_NATIVE_PLATFORM_TYPE_LINUX		= 3,
+
+	MONO_NATIVE_PLATFORM_TYPE_IPHONE	= 0x100,
+	MONO_NATIVE_PLATFORM_TYPE_TV		= 0x200,
+	MONO_NATIVE_PLATFORM_TYPE_WATCH		= 0x400,
+
+	MONO_NATIVE_PLATFORM_TYPE_COMPAT	= 0x1000,
+	MONO_NATIVE_PLATFORM_TYPE_UNIFIED	= 0x2000,
+
+	MONO_NATIVE_PLATFORM_TYPE_SIMULATOR	= 0x4000,
+	MONO_NATIVE_PLATFORM_TYPE_DEVICE	= 0x8000
+} MonoNativePlatformType;
+
+MONO_API int32_t
+mono_native_get_platform_type (void);

+ 1 - 1
mono/metadata/pal-icalls.c → mono/native/pal-icalls.c

@@ -12,7 +12,7 @@
 #include <glib.h>
 #include "mono/utils/mono-threads-api.h"
 #include "mono/utils/atomic.h"
-#include "loader-internals.h"
+#include "metadata/loader-internals.h"
 #include "pal-icalls.h"
 
 /*

+ 2 - 2
mono/metadata/pal-icalls.h → mono/native/pal-icalls.h

@@ -9,8 +9,8 @@
 #ifndef __MONO_METADATA_PAL_ICALLS_H__
 #define __MONO_METADATA_PAL_ICALLS_H__
 
-#include "metadata.h"
-#include "class-internals.h"
+#include "mono/metadata/metadata.h"
+#include "mono/metadata/class-internals.h"
 
 MONO_API void mono_pal_init (void);
 

+ 0 - 0
mono/metadata/pal_config.h → mono/native/pal_config.h


+ 3 - 0
mono/native/platform-type-compat.c.in

@@ -0,0 +1,3 @@
+#include "mono-native-platform.h"
+
+MonoNativePlatformType mono_native_platform_type = @MONO_NATIVE_PLATFORM_TYPE_COMPAT@;

+ 4 - 0
mono/native/platform-type-unified.c.in

@@ -0,0 +1,4 @@
+#include "mono-native-platform.h"
+
+MonoNativePlatformType mono_native_platform_type = @MONO_NATIVE_PLATFORM_TYPE_UNIFIED@;
+

+ 3 - 0
mono/native/platform-type.c.in

@@ -0,0 +1,3 @@
+#include "mono-native-platform.h"
+
+MonoNativePlatformType mono_native_platform_type = @MONO_NATIVE_PLATFORM_TYPE@;

+ 3 - 10
runtime/Makefile.am

@@ -216,19 +216,12 @@ etc/mono/config: ../data/config Makefile $(symlinks)
 	else \
 		echo "Unknown directory '$(abs_top_builddir)/support'" && false; \
 	fi
-	if test -d "$(abs_top_builddir)/mono/metadata"; then \
-		sed 's,target="$$mono_libdir/libmono-system-native$(libsuffix)",target="$(abs_top_builddir)/mono/metadata/libmono-system-native.la",' $@ > [email protected] \
+	if test -d "$(abs_top_builddir)/mono/native"; then \
+		sed 's,target="$$mono_libdir/$(MONO_NATIVE_LIBRARY_NAME)$(libsuffix)",target="$(abs_top_builddir)/mono/native/$(MONO_NATIVE_LIBRARY_NAME).la",' $@ > [email protected] \
 		 && mv [email protected] $@ \
 		 ; \
 	else \
-		echo "Unknown directory '$(abs_top_builddir)/mono/metadata'" && false; \
-	fi
-	if test -d "$(abs_top_builddir)/mono/metadata"; then \
-		sed 's,target="$$mono_libdir/libmono-system-net-security-native$(libsuffix)",target="$(abs_top_builddir)/mono/metadata/libmono-system-net-security-native.la",' $@ > [email protected] \
-		 && mv [email protected] $@ \
-		 ; \
-	else \
-		echo "Unknown directory '$(abs_top_builddir)/mono/metadata'" && false; \
+		echo "Unknown directory '$(abs_top_builddir)/mono/native'" && false; \
 	fi
 	if test -d "$(abs_top_builddir)/mono/btls/build-shared"; then \
 		sed 's,target="$$mono_libdir/libmono-btls-shared$(libsuffix)",target="$(abs_top_builddir)/mono/btls/build-shared/libmono-btls-shared$(libsuffix)",' $@ > [email protected] \

+ 1 - 0
sdks/ios/appbuilder/appbuilder.cs

@@ -294,6 +294,7 @@ public class AppBuilder
 				libs += " $mono_sdkdir/ios-target64-release/lib/libmono-icall-table.a";
 				libs += " $mono_sdkdir/ios-target64-release/lib/libmono-ilgen.a";
 			}
+			libs += " $mono_sdkdir/ios-target64-release/lib/libmono-native-unified.dylib";
 			ninja.WriteLine ($"build $appdir/{bundle_executable}: gen-exe {ofiles} $builddir/main.o " + libs + " $monoios_dir/libmonoios.a");
 			ninja.WriteLine ("build $builddir/main.o: compile-objc $builddir/main.m");
 		} else {

+ 1 - 0
sdks/ios/runtime/Makefile

@@ -34,6 +34,7 @@ SIM_LDFLAGS = \
 	$(foreach u,$(UNREFERENCED_SYMBOLS),-u $u)  \
 	$(SIM_SDK_DIR)/lib/libmonosgen-2.0.a \
 	$(SIM_SDK_DIR)/lib/libMonoPosixHelper.a \
+	-force_load $(SIM_SDK_DIR)/lib/libmono-native-unified.a \
 	-liconv -lz
 
 sim-%.o: %.m

+ 9 - 0
sdks/ios/runtime/runtime.m

@@ -204,6 +204,13 @@ log_callback (const char *log_domain, const char *log_level, const char *message
 	}
 }
 
+static void
+register_dllmap (void)
+{
+	mono_dllmap_insert (NULL, "System.Native", NULL, "__Internal", NULL);
+	mono_dllmap_insert (NULL, "System.Security.Cryptography.Native.Apple", NULL, "__Internal", NULL);
+}
+
 /* Implemented by generated code */
 void mono_ios_register_modules (void);
 void mono_ios_setup_execution_mode (void);
@@ -284,6 +291,8 @@ mono_ios_runtime_init (void)
 	const char *bundle = get_bundle_path ();
 	chdir (bundle);
 
+	register_dllmap ();
+
 #ifdef DEVICE
 	mono_ios_register_modules ();
 	mono_ios_setup_execution_mode ();