From f212f6bdb3d6fd8bb39ecbdaefde20a322ae2c8f Mon Sep 17 00:00:00 2001 From: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com> Date: Thu, 13 Jul 2023 16:57:37 +0200 Subject: [PATCH] Do not include -dead_strip when native linking a NativeAOT object file (#18553) This PR disables passing `-dead_strip` to the native linker in case of NativeAOT runtime to prevent build failures. Additionally, this change affects the size of the application in the following way - measured with `dotnet new maui` app version `8.0.0-preview.7.23359.1`: | MAUI iOS | -dead_strip | no -dead_strip | diff (b) | diff (Kb) | diff (%) | |----------|--------------|----------|----------|-----------|----------| | .ipa (b) | 13377583 | 13435276 | 57693 | 57,693 | 0,43% | | Size on disk (b) | 41883897 | 42038873 | 154976 | 154,976 | 0,37% | | binary (b) | 39614336 | 39769312 | 154976 | 154,976 | 0,39% | Even though the size of the application regresses, with this change we have a more stable product. Finally, once this PR gets merged we can open a tracking issue to solve the size regression either by fixing: - https://github.com/dotnet/runtime/issues/88032 or - by manually removing the dead code as proposed by @filipnavara here: https://github.com/dotnet/runtime/issues/88032#issuecomment-1624303167 --- Fixes: https://github.com/xamarin/xamarin-macios/issues/18552 --- tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs b/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs index 923218285f21..54c67098cd7d 100644 --- a/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs +++ b/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Xamarin.Utils; +using Xamarin.Bundler; #nullable enable @@ -51,7 +52,8 @@ protected override void TryEndProcess () Configuration.Application.DeadStrip = false; var mainLinkerFlags = new List (); - if (Configuration.Application.DeadStrip) { + // Do not pass -dead_strip to the native linker with object files generated by NativeAOT compiler as it can cause the linker to seg fault + if (Configuration.Application.DeadStrip && Configuration.Application.XamarinRuntime != XamarinRuntime.NativeAOT) { mainLinkerFlags.Add (new MSBuildItem ("-dead_strip")); } Configuration.WriteOutputForMSBuild ("_AssemblyLinkerFlags", mainLinkerFlags);